WPF - 图像“不是项目的一部分或其构建操作未设置为资源”

发布于 2024-07-15 03:19:12 字数 479 浏览 16 评论 0原文

我有一个项目需要在窗口中显示图像。 这是一个静态图像,我通过“添加>现有项目”添加。 它存在于项目的根目录中。

我像这样在测试页面中引用图像 -

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

老子叫无熙 2024-07-22 03:19:12

尝试进行完全重建,或删除构建文件,然后构建该文件。

Visual Studio 并不总是会接受对资源的更改,并且重新编译它可能会很痛苦。

还可以尝试使用完整的 URI,因为当我遇到同样的问题时这对我很有帮助。 就像是

pack://application:,,,/MyAssembly;component/bug.png

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
背叛残局 2024-07-22 03:19:12

→ 右键单击​​图像文件
→ 单击属性
→ 选择资源的构建操作
→ 清理和构建解决方案
→ 运行解决方案

您将获得全部内容。

→ Right click the image file
→ Click property
→ Select Build Action to Resource
→ Clean and Build solution
→ Run the Solution

You will get the all.

舟遥客 2024-07-22 03:19:12

我遇到过同样的问题。 清理和重建解决方案并没有解决它,所以我重新启动了 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.

请持续率性 2024-07-22 03:19:12

尝试以“/”开始图像的路径:

<Image Source="/bug.png"/>

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

<Image Source="/bug.png"/>
软糯酥胸 2024-07-22 03:19:12

您的问题有一个解决方案

<Image Source="/WpfApplication4;component/images/sky.jpg" />

“组件”不是文件夹!

There is a solution to your question

<Image Source="/WpfApplication4;component/images/sky.jpg" />

"component" is not a folder!

时间你老了 2024-07-22 03:19:12

它没有,或者至少当前的测试版没有。 我在研究完全相同的问题时发现了此页面。 重建/清理什么也没做。 关闭并重新加载解决方案后,文件神奇地再次兼容。

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.

热血少△年 2024-07-22 03:19:12

我遇到了完全相同的问题,但重新启动 VS2008 或清理和重建项目对我来说不起作用。 最后,以下步骤成功了。

  • 在 Windows 资源管理器中,将图像复制到项目资源文件夹中。 示例:MyProject\Resources\
  • 在 Visual Studio 中右键单击资源并选择“添加 > 现有项目”,然后选择刚刚在
  • 表单 XAML 中复制的图像,将图像源设置为:"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.

  • 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

Hope this helps someone

记忆消瘦 2024-07-22 03:19:12

异步加载示例,另一种选择。 示例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();
}

Example of async load, another option. Example clip.mp4 is in the web project root.

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();
}
把回忆走一遍 2024-07-22 03:19:12

我有类似的问题。 当我从解决方案资源管理器中删除了一个我并未真正使用的 someStyle.xaml 文件后。
然后我恢复了文件,但没有发生任何变化。 清理和重建项目没有帮助。

只需删除相应的行:

<ResourceDictionary Source="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:

<ResourceDictionary Source="someStyle.xaml"/> 

did the trick.

一梦等七年七年为一梦 2024-07-22 03:19:12

我有同样的错误消息,但我的问题是一个简单的 NOOB 错误。

当我将 .ico 文件添加到“我的项目/资源”时,VS 创建了一个名为 Resources 的子文件夹,我正在尝试使用;

<Window Icon="icons1.ico">

当我应该使用时;

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

...不要判断,我 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;

<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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文