WPF - 图像“不是项目的一部分或其构建操作未设置为资源”
我有一个项目需要在窗口中显示图像。 这是一个静态图像,我通过“添加>现有项目”添加。 它存在于项目的根目录中。
我像这样在测试页面中引用图像 -
<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>
问题是我收到一条消息,说找不到它或者它的构建操作不是资源,但它确实存在并且它的构建操作是资源。 如果我创建一个新应用程序并将其扔到窗口上,那么它就可以正常工作。
任何帮助都会很棒。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
尝试进行完全重建,或删除构建文件,然后构建该文件。
Visual Studio 并不总是会接受对资源的更改,并且重新编译它可能会很痛苦。
还可以尝试使用完整的 URI,因为当我遇到同样的问题时这对我很有帮助。 就像是
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
→ 右键单击图像文件
→ 单击属性
→ 选择资源的构建操作
→ 清理和构建解决方案
→ 运行解决方案
您将获得全部内容。
→ Right click the image file
→ Click property
→ Select Build Action to Resource
→ Clean and Build solution
→ Run the Solution
You will get the all.
我遇到过同样的问题。 清理和重建解决方案并没有解决它,所以我重新启动了 Visual Studio,它就解决了。 希望 Visual 2010 能够解决这个问题以及 Visual 2008 中困扰 wpf 的许多其他问题。
I had the same issue. Cleaning and rebuilding the solution didn't fix it so I restarted visual studio and it did. Here's hoping Visual 2010 fixes this issue and the many others that plauge wpf in Visual 2008.
尝试以“/”开始图像的路径:
Try starting the path to your image with a "/":
您的问题有一个解决方案
“组件”不是文件夹!
There is a solution to your question
"component" is not a folder!
它没有,或者至少当前的测试版没有。 我在研究完全相同的问题时发现了此页面。 重建/清理什么也没做。 关闭并重新加载解决方案后,文件神奇地再次兼容。
It doesn't, or at least the current beta doesn't. I found this page while looking into exactly the same problem. Rebuild/clean did nothing. After closing down and reloading the solution the file magically became compatible again.
我遇到了完全相同的问题,但重新启动 VS2008 或清理和重建项目对我来说不起作用。 最后,以下步骤成功了。
"Source ="Resources/MyImage.ico"
(我的图像被保存为图标(.ico)文件,但这种方法应该适用于任何图像类型希望这对某人有帮助
I faced the exact same issue but restarting VS2008 or cleaning and rebuilding the project did not work for me. In the end, the below steps did the trick.
"Source="Resources/MyImage.ico"
(my image was saved as icon (.ico) file, but this approach should work for any image typeHope this helps someone
异步加载示例,另一种选择。 示例clip.mp4位于Web项目根目录中。
Example of async load, another option. Example clip.mp4 is in the web project root.
我有类似的问题。 当我从解决方案资源管理器中删除了一个我并未真正使用的
someStyle.xaml
文件后。然后我恢复了文件,但没有发生任何变化。 清理和重建项目没有帮助。
只需删除相应的行:
就可以了。
I had a similar problem. After I deleted a
someStyle.xaml
file that I wasn't really using from the solution explorer.Then I restored the file, but no change happened. Cleaning and rebuilding the project did not help.
Simply deleting the corresponding row:
did the trick.
我有同样的错误消息,但我的问题是一个简单的 NOOB 错误。
当我将 .ico 文件添加到“我的项目/资源”时,VS 创建了一个名为 Resources 的子文件夹,我正在尝试使用;
当我应该使用时;
...不要判断,我 1 周前开始使用 WPF :)
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;
when I should have been using;
... don't judge, I started using WPF 1 week ago :)