【发布时间】:2009-04-02 09:58:01
【问题描述】:
我在文件系统的某处有一个程序集,例如“C:\temp\test.dll”。 在那个程序集中有一个 ResourceDictionary,例如“abc.xaml”。
我怎样才能得到那个 ResourceDictionary?也许有一种使用反射的方法?到目前为止我还没有找到解决方案。
提前致谢!
编辑:只是想添加我想访问字典中的资源,例如样式。
【问题讨论】:
标签: c# wpf xaml resourcedictionary
我在文件系统的某处有一个程序集,例如“C:\temp\test.dll”。 在那个程序集中有一个 ResourceDictionary,例如“abc.xaml”。
我怎样才能得到那个 ResourceDictionary?也许有一种使用反射的方法?到目前为止我还没有找到解决方案。
提前致谢!
编辑:只是想添加我想访问字典中的资源,例如样式。
【问题讨论】:
标签: c# wpf xaml resourcedictionary
你实际上需要这样写 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”。
【讨论】:
UriKind,如果您使用的是相对路径,则应提供UriKind.Relative
获取一份Reflector 的副本(Lutz 已将其移交)。用它来查看其中资源的程序集和命名空间等。
然后像这样读入嵌入式资源;
Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
using (System.IO.Stream s = asm.GetManifestResourceStream(<yourname>)
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(s))
{
string xml = reader.ReadToEnd();
}
}
【讨论】: