Loading and Parsing XAML at Runtime

The Windows.UI.Xaml.Markup namespace contains a simple XamlReader class with a simple static Load method. Load can parse a string containing XAML, create the appropriate objects, and return an instance of the root element. So, with a string containing XAML content somewhat like MainPage.xaml from the preceding chapter, the following code could be used to load and retrieve the root Page object:

string xamlString = ...; // Get the root element, which we know is a Page Page p = (Page)XamlReader .Load(xamlString);

After Load returns, the entire hierarchy of objects in the XAML file is instantiated in memory, so the XAML itself is no longer needed. Now that an instance of the root element ...

Get Universal Windows® Apps with XAML and C# Unleashed 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.