Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  

How to read WPF ResourceDictionaries from WinForms

When you add resources dictionaries to an WPF project the build action is automatically set to Page. This means that the compiler generates a BAML stream and adds it to the resources of the assembly.

Since WPF has the built-in functionality to read a BAML stream but its API is not public, we have to write a little helper class that access the internal method by using reflection.

public static class BamlReader
    public static object Load(Stream stream)
        ParserContext pc = new ParserContext();
        MethodInfo loadBamlMethod = typeof(XamlReader).GetMethod("LoadBaml", 
            BindingFlags.NonPublic | BindingFlags.Static)
        return loadBamlMethod.Invoke(null, new object[] { stream, pc, null, false });
StreamResourceInfo sri = System.Windows.Application.GetResourceStream(
    new Uri("/MyAssemblyName;component/MyResourceDict.xaml", UriKind.Relative));
    ResourceDictionary resources = (ResourceDictionary)BamlReader.Load(sri.Stream);

Last modified: 2009-07-10 13:39:59
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Commented on 28.February 2009
It would have been nice if you took a example and explained step by step. But still its a good article
Commented on 10.July 2009
Hi, could you explain me how to get/read the xaml content of the form I designed in C#? Please send me email if you could
Blood HaZaRd
Commented on 4.June 2010
That's a clear use of the BMALView. And for Goo, it's almost clear no need for a SbS Tutorial; all u have to do is to read about the BAMLView (google it or go to mocrosoft website)

E-Mail (optional)