从程序集加载ResourceDictionary
我在文件系统的某个地方有一个程序集,例如“C:\ temp \ test.dll”。 在那个程序集中有一个ResourceDictionary,例如“abc.xaml”。
我如何获得ResourceDictionary? 也许有一种方法使用Reflections? 到目前为止我没有找到解决方案。
提前致谢!
编辑:只想添加我想要访问字典中的资源,例如样式。
你真的需要像这样写Uri:
Assembly.LoadFrom(@"C:\temp\test.dll"); ResourceDictionary rd = new ResourceDictionary(); rd.Source = new Uri(@"pack://application:,,,/test;component/myresource.xaml");
编辑:我找到了一个更好的解决方案,适用于ResourceDictionaries:
Assembly.LoadFrom(@"C:\temp\test.dll"); ResourceDictionary rd = new ResourceDictionary(); rd.Source = new Uri("/test;component/myresource.xaml");
好吧,我无法使用ResourceDictionaries,所以我使用的是旧的资源文件;)对于任何感兴趣的人,我都是这样做的:
Assembly a = Assembly.LoadFile(@"C:\temp\test.dll"); ResourceManager rm = new ResourceManager("NameOfResource", a); object o = rm.GetObject("xyz");
正如Ian建议的那样,您可以使用Reflector获取“NameOfResource”。
抓住Reflector的副本(Lutz现在提交了这个)。 用它来查看其中资源的程序集和命名空间等。
然后在嵌入式资源中读取这样的内容;
Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); using (System.IO.Stream s = asm.GetManifestResourceStream() { using (System.IO.StreamReader reader = new System.IO.StreamReader(s)) { string xml = reader.ReadToEnd(); } }