Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #60 – How to load a XAML Control From a File or String

If you have a control written in XAML that is included in your project you can load and create it directly from file by using the method: System.Windows.Markup.XamlReader.Load().This method can also be used to directly create a Silverlight control from a string.

To demonstrate this I have created two functions called LoadFromXAML(). The first function takes takes as a parameter a URI that points to the XAML file in your project you want to load. The second takes as a parameter a string representation of the control.

public static object LoadFromXaml(Uri uri)
{
    System.Windows.Resources.StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(uri);
 
    if ((streamInfo != null) && (streamInfo.Stream != null))
    {
        using (System.IO.StreamReader reader = new System.IO.StreamReader(streamInfo.Stream))
        {
            return System.Windows.Markup.XamlReader.Load(reader.ReadToEnd());
        }
    }
 
    return null;
}
public static object LoadFromXamlString(string xamlControl)
{
    return System.Windows.Markup.XamlReader.Load(xamlControl);
}

The above methods return a generic object that can be typecast to the object you are loading. For example:

Button myButton = (Button)LoadFromXaml(new Uri("/LoadXaml;component/MyButton.xaml", UriKind.Relative));

or

string buttonXAML = "<Button xmlns='http://schemas.microsoft.com/client/2007' Width=\"100\" Height=\"100\" Content=\"Click Me\"></Button>";

Button myButton = (Button) LoadFromXaml(buttonXAML);

Note that in the XAML you must declare a default XML namespace as highlighted below:

<Button xmlns='http://schemas.microsoft.com/client/2007' Width="100" Height="100" Content="Click Me"></Button>

If you do not declare this namespace, you will see the following error:

AG_E_PARSER_MISSING_DEFAULT_NAMESPACE [Line: 0 Position: 0]

 

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

If you have a control written in XAML that is included in your project you can load and create it directly

# October 10, 2008 6:17 PM

unruledboy2 said:

return System.Windows.Markup.XamlReader.Load(reader.ReadToEnd());

better to be:

return LoadFromXamlString(reader.ReadToEnd());

:)

# October 10, 2008 10:07 PM

Dew Drop - October 11, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 11, 2008 | Alvin Ashcraft's Morning Dew

# October 11, 2008 9:29 AM

Dew Drop - October 11, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 11, 2008 | Alvin Ashcraft's Morning Dew

# October 12, 2008 6:07 PM

2008 October 13 - Links for today « My (almost) Daily Links said:

Pingback from  2008 October 13 - Links for today &laquo; My (almost) Daily Links

# October 13, 2008 1:44 AM

Silverlight news for October 13, 2008 said:

Pingback from  Silverlight news for October 13, 2008

# October 13, 2008 7:55 AM

#.think.in said:

#.think.in infoDose #3 (13th Oct - 17th Oct)

# October 19, 2008 6:16 PM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #66 Title: How to copy XAML for Silverlight from Expression Designer Silverlight

# November 3, 2008 1:41 PM

silverlight_kid said:

hi

WE Added dynamic control using XAMLREADER

then how to add eventhandler for that dynamically loaded control kinldy let me know

# November 27, 2008 2:19 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

DjoDjo said:

Thanks for this but, can you please post a demo project of what you're doing.

I mean, for my use I have a xaml file but I can't load it.

thanks

# January 14, 2009 4:52 AM

??????????????? ??? ?????? « jin_u as blog said:

Pingback from  ??????????????? ??? ?????? &laquo; jin_u as blog

# March 24, 2009 10:02 PM

Grey Matter said:

DjoDjo, make the following steps:

1. Click on your project, and go to Add->Existing Item.. and then chose your Button.xaml file. It must contain something like this:

<Button xmlns='schemas.microsoft.com/.../2007' Height="100" Content="Button From Xaml"/>

2. Now when your XAML file's been added to your project click on it to open it's properties.

3. In the Properties pane chose option "Resource" for the "Build Action" field.

Now it has to work well.

# July 2, 2009 4:48 PM

Xusan said:

Great thank.

# November 26, 2009 1:13 AM

mashumafi said:

this does not work for me, the stream is always null, does the file need to be set as resource or anything?

# December 19, 2010 12:33 PM