Seva Alekseyev
Seva Alekseyev

Reputation: 61378

Where do XAML files that build as "Page" go?

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.

Upvotes: 2

Views: 391

Answers (1)

devdigital
devdigital

Reputation: 34359

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.LoadComponent which is what the InitializeComponent method uses internally. InitializeComponent is called by the partially generated class for your XAML code behind.

var uri = new Uri("/MyAppName;component/MyXaml.xaml", //Note extension: XAML, not BAML
     UriKind.Relative);
Page rootObject = new Page(); //Assuming XAML root element is Page - it could be anything
Application.LoadComponent(rootObject, uri);

(assuming the root element of your XAML file is a Page).

You can then serialize the Page to a XAML string using the XamlWriter:

string xaml = XamlWriter.Save(rootObject);

Note that this is the XamlWriter in the System.Windows.Markup namespace, not System.Xaml. If your XAML has WPF types, then you should use this XamlWriter to avoid errors.

Upvotes: 3

Related Questions