Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Haii Friends..


Is It Possible to Load a XAML file As it is and Make it a UI but Not it's Content to another File at Run time. I came across with solutions That works with content of XAML at Runtime.XAMLWRITER and READER do the Work..
Grid grd = new Grid();
                grd.Height = 210;
                grd.Width = 400;
                Button btn = new Button();
                btn.Height = 50;
                btn.Width = 80;
                btn.Content = "Dyn. Button";
                btn.Background = new SolidColorBrush(Colors.Red);               
                btn.Margin = new Thickness(5, 5, 310, 120);
                grd.Children.Add(btn);

                TextBox txt = new TextBox();
                txt.Height = 50;
                txt.Width = 100;
                txt.Text = "Dynamic TextBox";
                txt.Foreground = new SolidColorBrush(Colors.Red);
                txt.Margin = new Thickness(5, 60, 310, 80);
                grd.Children.Add(txt);

                //Code to handle Cs management.
                System.Xaml.XamlReader aa=null;
                Page page = (Page)System.Windows.Markup.XamlReader.Load(aa);

                TextBox tb = page.FindName("firstName") as TextBox;

                if (tb != null)
                    tb.Text = "Joe";

                btnLoadXAML = page.FindName("btnLoadXAML") as Button;

                if (btnLoadXAML != null)
                    btnLoadXAML.Click += new RoutedEventHandler(btnLoadXAML_Click); // andaddthe btnOk_Clickevent handler below

                //Store this Xaml in File

                FileStream Fs = new FileStream(@"E:\GeneratedFiles\MyDesign.Xaml",
                    FileMode.CreateNew);
                System.Windows.Markup.XamlWriter.Save(grd, Fs);



This is the code am using to create XAML.. but i have to Add CS file along with XAML and at run time i need to load it together.Can Anybody help me.. and also is it possible to keep the cs file Separately and link them at run time..
Thank you..
Posted
Updated 27-Apr-12 1:32am
v2
Comments
S@53K^S 27-Apr-12 10:54am    
Is it mandatory that you have to load external file to display the UI or you can use the user controls and load the same into the main file.As loading the ui from external file may be performance degrading.
anish.karunakaran 28-Apr-12 0:06am    
Haii.. Ya for the First time only Am creating the xaml files From DB. After That what needed is only to Load the File. Is it Possible to Save CS file Separately and link in at loading time..
S@53K^S 9-May-12 10:32am    
Is it resolved ?If not Just one question why are trying redo the compiler actions again ? and XAML is a windows app once you compile it becomes a DLL or exe that can be used.and what you are doing will deteriorate the performance of the application and make it hard to debug it.

1 solution

Hi Anish.... this demo code extends TextBlock and builds a new TextBlock from a string (no code file needed):

C#
public class TextoRecurso : TextBlock
{
  public string Recurso
  {
    set
    {
      Inlines.Clear();
      Text = null;
 
      if (value == null) return;
      StringBuilder SB = new StringBuilder();
 
      SB.Append("<textblock xmlns="\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"">");
      SB.Append(value);
      SB.Append("</textblock>");
 
      var TB =
        XamlReader.Load(new System.Xml.XmlTextReader(
          new System.IO.StringReader(SB.ToString()))) as TextBlock;
      var Lista = TB.Inlines.ToArray();
 
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900