0

I have a WPF Application that contains a xaml file marked as ressource (buildprocess). During runtime I can load and access that file using following code:

Uri uri = new Uri("some.xaml", UriKind.Relative);
System.Windows.Resources.StreamResourceInfo sri = Application.GetResourceStream(uri);
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
Border savedBorder = reader.LoadAsync(sri.Stream) as Border;

Moving the functionality and xaml file into a class library and mark "some.xaml" it as resource, the above code fails with IOException "The resource "some.xaml" could not be found". That sounds logically because I cannot successfully access Application.GetResourceStream inside a class library.

System.Reflection.Assembly
   .GetExecutingAssembly()
   .GetManifestResourceStream("some.xaml") ;

returns null.

What is the correct way to Access such a file inside a dll?

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
Matthias Fuchs
  • 121
  • 2
  • 12

3 Answers3

0

Your resource name is incorrect. Visual Studio creates the resource with the fully qualified name (e.g. the namespace and the resource name).

See How to read embedded resource text file

Try something like:

 System.Reflection.Assembly
   .GetExecutingAssembly()
   .GetManifestResourceStream("mycompany.myproduct.some.xaml");
Community
  • 1
  • 1
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • Thanks for the advice. I tried getting the full resource name by using 'this.GetType().Assembly.GetManifestResourceNames();' The result is 'OWM.g.resources'. "OWM" is my root Namespace. Passing this value into 'GetManifestResourceStream' an UnmanagedMemoryStream is returned. Much larger than the expected filesize. – Matthias Fuchs Apr 24 '14 at 13:40
  • Is it twice than expected size? If so its saved in UTF-16 not UTF-8. But are you able to read it now? – Richard Schneider Apr 24 '14 at 13:49
  • No worries, if you like the answer then please accept it. – Richard Schneider Apr 24 '14 at 14:10
0

I struggeled with full resource names. This code solved the Problem:

Assembly assembly = GetExecutingAssembly(); 
using (var stream = assembly.GetManifestResourceStream(this.GetType(), "some.xaml")) 
{ 
   StreamReader sr = new StreamReader(stream); 
   string Content = sr.ReadToEnd(); 
} 

So there is no need to specify the full resource Name.

Matthias Fuchs
  • 121
  • 2
  • 12
0

Both answers are right, but I have to notice one imprecision: the resource name should be in another format.

var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("UCM.WFDesigner.Resources.Flows.DefaultFlow.xaml");

UCM.WFDesigner is my assembly name, Resources.Flows is the folder name, and DefaultFlow.xaml is the xaml name.

Mr.B
  • 3,484
  • 2
  • 26
  • 40