Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all XAML files from compiled DLL

Tags:

c#

wpf

xaml

I want to load during runtime external XAML styles from third-party libraries (DLLs). Like in this tutorial they use:

Application.LoadComponent(new Uri("/WpfSkinSample;component/Skins/" + name + ".xaml", UriKind.Relative)) as ResourceDictionary;

to load the new style.

But I don't know the XAML names from the third-party library, so I'm searching for a way to get them and load them into my application.

Thanks for any help.

Edit: Thanks to andyp, I did the following work out:

    public void LoadXaml(String Assemblypath)
    {
        var assembly = Assembly.LoadFile(Assemblypath);
        var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".g.resources");
        var resourceReader = new ResourceReader(stream);

        foreach (DictionaryEntry resource in resourceReader)
        {
            if (new FileInfo(resource.Key.ToString()).Extension.Equals(".baml"))
            {
                Uri uri = new Uri("/" + assembly.GetName().Name + ";component/" + resource.Key.ToString().Replace(".baml", ".xaml"), UriKind.Relative);
                ResourceDictionary skin = Application.LoadComponent(uri) as ResourceDictionary;
                this.Resources.MergedDictionaries.Add(skin);
            }
        }
    }
like image 671
Briefkasten Avatar asked Mar 30 '14 17:03

Briefkasten


1 Answers

You can enumerate the embedded resources of an assembly like this:

var assembly = Assembly.LoadFile("pathToYourAssembly");
var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".g.resources");
var resourceReader = new ResourceReader(stream);

foreach (DictionaryEntry resource in resourceReader)
    Console.WriteLine(resource.Key);
like image 197
andyp Avatar answered Oct 15 '22 00:10

andyp