Is this possible
I'm Beginner in winFX programming and I want to load a xaml file dynamically.
For example I make an event handler which load the new Xaml Description from a uri (generated by another application) on event catched and load the new interface in a panel.
Have you an example or a tutorial on it
Thanks for your help ![]()

Load XAML File dynamically
Angela Zhou
I am trying to create a XBAP application and I am having problems defining proper permissions to read a XAML file.
Also, I tried this code using just a XML string, the code goes through but the Page does not change, I do not see the new controls loaded
How do you load a custom loaded page on the current page Or do you have to navigate to the custom loaded page, if so how
Thank you for your help.
SharpDreamer
this.Content = XamlReader.Load(stream);
Cesar.NET
Hi Ashish,
Can you give an example for the 2nd option - using custom page with component Thanks!
Pankaj Sabnis
Thanks,
I tried this sample but it can only load FlowDocument, what I want to do is to load a panel with Button, Label etc.
I'll try to modify the sample.
Divyajosephjohn
There is no support for dynamically loading (or compiling) code behind or inline code with loose XAML.
There are a couple ways you can work around this:
e.g something like:
Page page = (Page)System.Windows.Markup.XamlReader.Load(xmlReader);
TextBox tb = page.FindName("firstName") as TextBox;
if (tb != null)
tb.Text = "Joe";
Button btnOk = page.FindName("btnOk") as Button;
if (btnOk != null)
btnOk.Click += new RoutedEventHandler(btnOk_Click); // and add the btnOk_Click event handler below
or
Iouri Simernitski - MSFT
Benin
Have a look in the SDK WinFX samples for FlowDocument_LoadSave sample. It does more or less what you need I think.
Michael
Pavel A.
Big thanks !!!!
this is what i need !
LuisFPF
I wish to create XAML files and have a single code behind for all the XAML files. How can I achieve this This will help me in creating an engine which will contain the code for a common code behind and I would be able to introduce multiple XAML files to the engine and they would run with all its event handlers being picked from the common code behind. Please help me out on this.
jloh
Thanks :)
I use XamlReader and it work but not completely, it "draw" my interface but don't rely with "functions".
This a example of my problem :
I have a cs file with a function which is described here :
private void CompileXaml(String xamlDocument)
{
StringReader stringReader = null;
XmlTextReader xmlReader = null;
try
{
stringReader = new StringReader(xamlDocument);
xmlReader = new XmlTextReader(stringReader);
UIElement documentRoot = (UIElement)System.Windows.Markup.XamlReader.Load(xmlReader);
interactionWindow.child = documentRoot;
}
catch (Exception ex)
{}
finally
{
if (stringReader != null) stringReader.Close();
if (xmlReader != null) xmlReader.Close();
}
}
private void Close(object sender, RoutedEventArgs e) { this.Close(); }
and i want to load dynamically the xaml description with xamlReader ( for example <Button Name="btnOk" Click="Close"> or more complicated interaction ) with xamlreader, I can just load <Button Name="btnOk">
it says "you must compile", I'm really obligated
Justahobby
ResourceDictionary rd = null;
rd = Application.LoadComponent( new Uri( uri, UriKind.RelativeOrAbsolute ) ) as ResourceDictionary;
if( rd != null )
Application.Current.Resources = rd;
I found this through Alan Le's blog post.