Question

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?

Was it helpful?

Solution

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");

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top