WPF resource file cannot be found

Encountered two problems, the first one is in the xaml file, the Source reference path in Image cannot be found,

The second is that the URI cannot be found in the cs file.

 

Analyze:

 

Separate a part from a solution as a separate project and compile it into a dll. If the executable program is generated in the original solution, there will be no problem, but after the separation, there is a problem that the resource file cannot be found.

 

According to the clues provided by netizens, the solution is as follows:

 

1. Xaml was written like this before

<Image Source="XXX;component/Resources/xx.img">

Here XXX represents the namespace of the executable file. When the dll project is generated separately, it is modified to the namespace of the project, but it does not work, and the picture cannot be loaded;

What to do, modify it to this

<Image Source="pack://application:,,,/Resources/xx.img">

It still doesn't work. Although you can see that the image is loaded in the designer, it is not when it is executed.

Finally, I would like to thank netizens for their ideas for combining the two:

<Image Source="pack://application:,,,XXX;component/Resources/xx.img">

That's it.

 

2. In the cs file, it was written like this before

URI uri = new URI("pack://application:,,,/Resources/xx.img");

Nothing. An exception occurred during operation and resources were not found. I tried many methods. When I was depressed, the majority of netizens had ideas again, and the combination worked, as follows:

URI uri = new URI("pack://application:,,,XXX;component/Resources/xx.img");

get, it's done.

 

For the time being, let's do this first, and then systematically summarize the resource this thing later.

Guess you like

Origin blog.csdn.net/kakaluote81/article/details/89476232