14.15. Loading XAML Dynamically

Silverlight allows you to load XAML at runtime. You might want to do this to reduce your application size, spread out loading through your application's lifetime, or dynamically load content. When you have retrieved your XAML content as a string, you need to pass it into the XamlReader.Load() method. All that is then necessary to do is cast the results of XamlReader.Load() to the type of object the XAML represents.

When constructing the XAML, be sure to add the xmlns namespace property to your objects; otherwise, Silverlight will be unable to determine the type of object you are trying to create. The following is a simple example illustrating loading a TextBlock from a string that would be placed in the code behind ...

Get Introducing .NET 4.0: with Visual Studio 2010 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.