Please visit DEMANDDRAFT.STORE for quality of products...

Ad

Search This Blog

Saturday, July 21, 2012

Can BAML be decompiled back into XAML?

Can BAML be decompiled back into XAML?
Yes, because an instance of any public .NET class can be serialized as XAML, regardless of
how it was originally declared. The first step is to retrieve an instance that you want to be the
root. If you don’t already have this object, you can call the static

System.Windows.Application.LoadComponent method as follows:

System.Uri uri = new System.Uri(“MyWindow.xaml”, System.UriKind.Relative);

Window window = (Window)Application.LoadComponent(uri);

This differs from previous code that uses FileStream to load a .xaml file because with LoadComponent, the name specified as the Uniform Resource Identifier (URI) does not have
to physically exist as a standalone .xaml file. 

LoadComponent can automatically retrieve BAML embedded as a resource when given the appropriate URI (which, by MSBuild convention,is the name of the original XAML source file).In fact, Visual Studio’s autogenerated InitializeComponent method calls Application.LoadComponent to load embedded BAML,although it uses a different overload.

After you’ve gotten a hold of the root element instance, you can use the System.Windows.Markup.XamlWriter class to get a XAML representation of the root element (and, therefore, any of its children). XamlWriter contains five overloads of a static Save method, the simplest of which accepts an object instance and returns appropriate XAML as a string. 

For example:
string xaml = XamlWriter.Save(window);

It might sound a little troubling that BAML can be so easily “cracked open,” but it’s really no
different from any other software running locally or displaying UI locally. (For example, you can
easily dig into a website’s HTML, JavaScript, and Cascading Style Sheets [CSS] files.)


No comments:

Post a Comment