WPF - 图像“不是项目的一部分或其构建操作未设置为资源”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/638845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - Image 'is not part of the project or its Build Action is not set to Resource'
提问by EightyOne Unite
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.
任何帮助都会很棒。
回答by Cameron MacFarland
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.
Visual Studio 并不总能获取对资源的更改,重新编译它可能会很痛苦。
Also try using a full URI as that helped me when I had the same problem. Something like
也尝试使用完整的 URI,因为当我遇到同样的问题时,这对我有帮助。就像是
pack://application:,,,/MyAssembly;component/bug.png
回答by sona jha
→ Right click the image file → Click property → Select Build Action to Resource → Clean and Build solution → Run the Solution
→右键单击图像文件→单击属性→选择资源的构建操作→清理并构建解决方案→运行解决方案
You will get the all.
你会得到一切。
回答by brianstewey
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.
我遇到过同样的问题。清理和重建解决方案并没有解决它,所以我重新启动了visual studio,它确实解决了。希望 Visual 2010 能够解决这个问题以及许多其他困扰 Visual 2008 中 wpf 的问题。
回答by Ben Gribaudo
Try starting the path to your image with a "/":
尝试使用“/”开始图像的路径:
<Image Source="/bug.png"/>
回答by towrywang
There is a solution to your question
你的问题有解决方案
<Image Source="/WpfApplication4;component/images/sky.jpg" />
"component" is not a folder!
“组件”不是文件夹!
回答by towrywang
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.
它没有,或者至少当前的测试版没有。我在研究完全相同的问题时发现了这个页面。重建/清理什么也没做。关闭并重新加载解决方案后,该文件神奇地再次兼容。
回答by pigleg
Example of async load, another option. Example clip.mp4 is in the web project root.
异步加载示例,另一种选择。示例 clip.mp4 位于 Web 项目根目录中。
void Landing_Loaded(object sender, RoutedEventArgs e)
{
//Load video async
Uri pageUri = HtmlPage.Document.DocumentUri;
Uri videoUri = new UriBuilder(pageUri.Scheme, pageUri.Host, pageUri.Port, "clip.mp4").Uri;
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(videoUri);
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
byte[] VideoBuffer = new byte[e.Result.Length];
e.Result.Read(VideoBuffer, 0, (int)e.Result.Length);
MemoryStream videoStream = new MemoryStream(VideoBuffer);
ContentVideo.SetSource(videoStream);
ContentVideo.Stop();
ContentVideo.Play();
}
回答by P_Fitz
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.
我遇到了完全相同的问题,但重新启动 VS2008 或清理和重建项目对我不起作用。最后,以下步骤成功了。
- In Windows Explorer copy the Image into your project resource folder. Example: MyProject\Resources\
- From within Visual Studio right click on the Resources and select "Add > Existing item" and select the image which you have just copied in
- From within the form XAML set the image source as:
"Source="Resources/MyImage.ico"
(my image was saved as icon (.ico) file, but this approach should work for any image type
- 在 Windows 资源管理器中,将图像复制到您的项目资源文件夹中。示例:MyProject\Resources\
- 在 Visual Studio 中右键单击资源并选择“添加 > 现有项目”并选择您刚刚复制的图像
- 从表单 XAML 中将图像源设置为:(
"Source="Resources/MyImage.ico"
我的图像已保存为图标 (.ico) 文件,但这种方法适用于任何图像类型
Hope this helps someone
希望这有助于某人
回答by JnuO
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.
我有一个类似的问题。在我someStyle.xaml
从解决方案资源管理器中删除了一个我并没有真正使用的文件之后。然后我恢复了文件,但没有发生任何变化。清理和重建项目没有帮助。
Simply deleting the corresponding row:
只需删除相应的行:
<ResourceDictionary Source="someStyle.xaml"/>
did the trick.
成功了。
回答by T4roy
I had the same error message but my issues was a simple NOOB mistake.
我有同样的错误消息,但我的问题是一个简单的 NOOB 错误。
When I added my .ico files to "My Project / Resources", VS made a sub folder named Resources and I was trying to use;
当我将我的 .ico 文件添加到“我的项目/资源”时,VS 创建了一个名为 Resources 的子文件夹,我正在尝试使用;
<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 :)
...不要判断,我一周前开始使用 WPF :)