I’ve added a XAML file to a Windows Phone 8 project. Its build action is “Page”. I want to load the XAML as a text string (to feed into XamlReader.Load()). How can I accomplish this?
It’s not available as a separate file in the XAP package; it’s probably somewhere in the DLL.
When set to
Page, the compiler will compile the XAML into BAML and add the BAML file as a resource to the assembly.If you wish to get the original XAML back out from the BAML resource at runtime, then you will need to deserialize the BAML, and then serialize your object to XAML.
You can have a look at the Baml2006Reader, or a better option would be to use
Application.LoadComponentwhich is what theInitializeComponentmethod uses internally.InitializeComponentis called by the partially generated class for your XAML code behind.(assuming the root element of your XAML file is a
Page).You can then serialize the
Pageto a XAML string using theXamlWriter:Note that this is the
XamlWriterin theSystem.Windows.Markupnamespace, notSystem.Xaml. If your XAML has WPF types, then you should use thisXamlWriterto avoid errors.