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