WPF throws “Cannot locate resource” exception when

2019-01-18 08:21发布

问题:

I am having a WPF window, with a background image of one file in my local system. So the XAML file looks like this:

 <Window.Background>
    <ImageBrush ImageSource="/MYASSEMBLY;component/Resources/MyPic.png"/>
 </Window.Background>

In the XAML designer it actually shows this background image, so the image does exist in MYASSEMBLY\Resources folder. However, in the InitializeComponent() function call when I launch the application, it throws the exception

Cannot locate resource 'resources/mypic.png'.

So I am totally lost now. Are the directories different between the design and runtime so doesn't exist in the runtime?

回答1:

In the solution explore, set the image "Build Action" property to "Resource" ,then clean the solution and rebuild it.



回答2:

WPF throws “Cannot locate resource” exception, this issue appeared when I was doing localisation using locbaml tool, when i compile i was getting this error. There were lots of resource merged dictionary reference So setting Build Action property of the resource xaml file to "Resource" from "Page" did resolve the issue.



标签: wpf resources