9
我想在運行時從第三方libaries(dll)中加載外部xaml樣式。像在本教程http://www.c-sharpcorner.com/UploadFile/raj1979/how-to-load-skins-dynamically-in-wpf/中那樣使用從編譯的dll獲取所有xaml文件
Application.LoadComponent(new Uri("/WpfSkinSample;component/Skins/" + name + ".xaml", UriKind.Relative)) as ResourceDictionary;
加載新樣式。
但我不知道第三方庫的xaml名稱,所以我正在尋找一種方法來獲取它們並將它們加載到我的應用程序中。
感謝您的任何幫助。
編輯: 感謝andyp,我做了以下工作:
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);
}
}
}
看ILSpy。 – SLaks
謝謝,使用你的代碼來確定什麼是神奇的URI。 – R4cOON