Load a ResourceDictionary from an assembly
Solution 1
Edit: I found an even better solution which works with ResourceDictionaries:
Assembly.LoadFrom(@"C:\temp\test.dll");
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri("/test;component/myresource.xaml");
Well, I couldn't get it to work with ResourceDictionaries, so I'm using good old Resource Files instead ;) For anyone interested, here is how I did it:
Assembly a = Assembly.LoadFile(@"C:\temp\test.dll");
ResourceManager rm = new ResourceManager("NameOfResource", a);
object o = rm.GetObject("xyz");
You can get "NameOfResource" with Reflector, as Ian suggested.
Solution 2
Grab a copy of Reflector (Lutz has handed this over now). Use that to look at the assembly and the namespace etc of the resources in it.
Then read in the embedded resource something like this;
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();
}
}
C.J. Morrison
Updated on July 09, 2022Comments
-
C.J. Morrison almost 2 years
I've got an assembly somewhere on the file system, e.g. "C:\temp\test.dll". In that assembly there's a ResourceDictionary, e.g. "abc.xaml".
How can i get that ResourceDictionary? Maybe there is a way using Reflections? I didn't find a solution so far.
Thanks in advance!
Edit: Just wanted to add that I want to access the Resources in the Dictionary, e.g. a Style.
-
C.J. Morrison about 15 yearsThank you. Unfortunatly, when I use your code, I'm just getting garbage from reader.ReadToEnd()
-
Dead account about 15 yearsI've no idea what XAML looks like, but I guess you need a decoder of some sort?
-
C.J. Morrison about 15 yearsI don't know, but I did it now with resource files. Thanks for your help!
-
Dead account about 15 years+1 always good to come back and tell us how you got on with your problem :)
-
Esben von Buchwald over 14 yearsSee Claraoscura's answer, it is a better solution.
-
aruno over 13 years@christian - the garbage is BAML not XAML : blogs.microsoft.co.il/blogs/tomershamam/archive/2007/05/25/…3D00-BAML-not-IL.aspx
-
ANewGuyInTown almost 9 yearsYou are missing the
UriKind
in your answer, if you are using relative path, you should supplyUriKind.Relative