从程序集加载ResourceDictionary

Chr*_*ann 16 c# wpf xaml resourcedictionary

我在文件系统的某个地方有一个程序集,例如"C:\ temp\test.dll".在那个程序集中有一个ResourceDictionary,例如"abc.xaml".

我怎样才能获得ResourceDictionary?也许有一种方法使用Reflections?到目前为止我没有找到解决方案.

提前致谢!

编辑:只是想添加我想要访问字典中的资源,例如样式.

小智 21

你真的需要像这样写Uri:

Assembly.LoadFrom(@"C:\temp\test.dll");
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri(@"pack://application:,,,/test;component/myresource.xaml");
Run Code Online (Sandbox Code Playgroud)

  • 愚蠢的Uri语法的文档在这里:http://msdn.microsoft.com/en-us/library/aa970069.aspx (5认同)

Chr*_*ann 10

编辑:我找到了一个更好的解决方案,适用于ResourceDictionaries:

Assembly.LoadFrom(@"C:\temp\test.dll");
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri("/test;component/myresource.xaml");
Run Code Online (Sandbox Code Playgroud)

好吧,我无法使用ResourceDictionaries,所以我使用的是旧的资源文件;)对于任何有兴趣的人,我都是这样做的:

Assembly a = Assembly.LoadFile(@"C:\temp\test.dll");
ResourceManager rm = new ResourceManager("NameOfResource", a);
object o = rm.GetObject("xyz");
Run Code Online (Sandbox Code Playgroud)

正如Ian建议的那样,您可以使用Reflector获取"NameOfResource".

  • +1总是很好回来告诉我们你如何继续你的问题:) (3认同)