WPF - Image 'is not part of the project or its

2019-02-04 01:26发布

I have a project which requires an image in the window. This is a static image and i added through 'Add>Existing Item'. It exists in the root of the project.

I reference the image in a test page like so -

<Page x:Class="Critter.Pages.Test"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Test">
      <Image Source="bug.png"/>
</Page>

Problem is I get a message saying it can't be found or it's build action isn't resource but it DOES exist and it's build action IS resource. If i create a new application and just throw it on a window then it works fine.

Any help would be great.

10条回答
可以哭但决不认输i
2楼-- · 2019-02-04 02:16

Try doing a full rebuild, or delete the build files and then build the file.

Visual Studio doesn't always pick up changes to resources, and it can be a pain to get it recompile.

Also try using a full URI as that helped me when I had the same problem. Something like

pack://application:,,,/MyAssembly;component/bug.png
查看更多
做个烂人
3楼-- · 2019-02-04 02:16

Try starting the path to your image with a "/":

<Image Source="/bug.png"/>
查看更多
走好不送
4楼-- · 2019-02-04 02:17

--> Right click the image file --> Click property --> Select Build Action to Resource --> Clean and Build solution --> Run the Solution You will get the all

Enjoy

查看更多
叛逆
5楼-- · 2019-02-04 02:17

I had the same error message but my issues was a simple NOOB mistake.

When I added my .ico files to "My Project / Resources", VS made a sub folder named Resources and I was trying to use;

<Window Icon="icons1.ico">

when I should have been using;

<Window Icon="Resources/icons1.ico">

... don't judge, I started using WPF 1 week ago :)

查看更多
登录 后发表回答