I have thought that in the future to write more posts in English. I know that all my Swedish readers have no problem with it.
Sometimes you want more flexibility to adapt the user interface for the data you download from a Web service. With dynamic loading of XAML one can both send data and a description of how this data should be displayed. With templates, you do not build the user interface and insert the data in the on the server side. One can instead bind the user interface and the data on the client side.
In this article I will show you how to load XAML from a local xml file. (In reality, this xml obviously come from a Web service.) I also want to show you how to get bindings to work to show the data.
The important method to get this to work is called XamlReader.Load, which takes a string of valid XAML code and creates an object structure of it.
With valid XAML code means:
- The XAML content string must define a single root element.
- The content string XAML must be well formed XML, as well as being parsable XAML.
- The required root element must overpriced specify a default XML namespace value. This is typically the Windows Phone namespace, xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation". This XML namespace is explicitly required in Windows Phone.
One can make use of well-formed XML to describe XAML. In my example, it means that I create a standard XML document and this creates my XAML.
I would emphasize again that one needs to have the attribute xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation in the root element object.
The example can be downloaded here.
Read XML file
Reading of the XML file is encapsulated in a method named loadXamlFromFile. It returns string because XmlReader.Load reads a string.
Create the data structuer that will be bind to the template
This is done in a simple way. The information consists of two classes, one of the station (StationData) and one for the traffic (TraficInformation).
And store it in a variable with the name of vm, for ViewModel.
Do the magic
The app has an event handler registered (commandLoadDynamicUI) for a buttons click event.
 call the method that loads the XML file and returns a string which is saved into the variable metroTemplate. At row  we call XamlReader.Load with that string and we get an Object variable is returned. [6 - 14] is the loading of the data that I described before.
At row  I add the XAML object structure, that XamlReader.Load returned, into an existing object tree. I have chosen to use a Grid as this object, but you can use any xaml objects that have a Children property.
Row  sets the DataContext for the root element object that was defined in the XML file.