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);
}
}
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With