When developing a temporary package, uri is valid, but not at runtime?

I set the Button content to the image. It looks something like this:

<Button> <Image Source="pack://application:,,,/NavigationImages/nav_up_left.png" /> </Button> 

In my project, I have a subfolder named NavigationImages, and inside this folder is the image file nav_up_left.png.

When I look at the constructor, an image appears, however at runtime I get an IOException error stating that I cannot find the resource.

Build action is set to Resource.

Actually, it worked perfectly in one project. But when I copied it on another project, it fails. This seems like an incredibly simple problem, but I'm at a standstill and ready to start pulling my hair out. @_ @

Your thoughts and suggestions will be highly appreciated!

+8
image wpf
source share
4 answers

Whelp, I realized that ... kinda.

I copied this xaml code from one project where the output type is a Windows application into another project where the output type is a class library.

I did not think about this at the time, but apparently when the output type is a class library, the package URI needs to be changed.

So, instead of "pack://application:,,,/NavigationImages/nav_up_left.png" I changed it to "/ProjectName;component/NavigationImages/nav_up_left.png" , and now it works fine.

I do not understand 100% why this works, and not the first. I read the MSDN documentation on the package URI in WPF , but maybe I misinterpreted something.

I will leave this answer unchecked if someone can give me a good explanation why what I had before does not work in a project with type Class Class.

I probably missed something very simple. @_ @

+9
source share

I struggled with this problem for quite some time, and I think that part of what went wrong in the original was the missing word “component”. For example, I had

 myBitmapImage.UriSource = new Uri(@"pack://application:,,,/MyApp;images/mona2.jpg"); 

but had to

 ... = new Uri(@"pack://application:,,,/MyApp;component/images/mona2.jpg"); 

The word "component" is not part of the path, despite its appearance - it is a string literal that should be there. What for? Someone thought it would be a good idea, I think.

And for those struggling with the other part of the thing, what about "MyApp"? This is the name of the Assembly. Right-click the name of your project, select "Properties ..." and on the "Application" tab you will see the "Assembly Name:" field.

If you don’t feel like looking for this (or worrying that this might change by breaking your code), you can do this:

 String appUri = @"pack://application:,,,/" + System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ";"; String path = appUri + "component/images/mona2.jpg"; myBitmapImage.UriSource = new Uri(path); 

The code is not very beautiful, I admit, it can be clearly shortened, but I hope it takes you where you need it. Remember to set the "Build" property in the image file to "Resource"!

+3
source share

Just to highlight what happened in your situation. Second uri package. The one that worked. Designed for resources located in the assembly, other than the host application. From the sounds of this host application, did you download this resource from the class library in question?

Here you can see the differences in uri packet schemes: MSDN package URI scheme

Uri changes slightly when referring to a resource from the main assembly and refers to one from another assembly.

In addition, the package: // application :, includes what is called "authority", omitting it basically makes it a relative path, both are valid in most cases where application authority is supposed to be applied.

EDIT: mainly because /Subfolder/Resource.xaml(.jpg, etc.) and /Assembly;component/Resource.xaml are very similar, the latter tells the parser / loader that it looks in the referenced assembly and not in The main assembly of the application. (I think this helps speed up the search).

+2
source share

Another solution for this:

After your Build Action image is set to "Resource" and you rebuilt it, go to the properties of your <Image /> object. The properties window will provide a browser ... of a file resource, after which your image will be correctly filled with the Source="..." attribute of your <Image /> .

+1
source share

All Articles