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条回答
再贱就再见
2楼-- · 2019-02-04 01:54

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

查看更多
唯我独甜
3楼-- · 2019-02-04 01:59

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.

查看更多
Evening l夕情丶
4楼-- · 2019-02-04 02:04

There is a solution to your question

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

"component" is not a folder!

查看更多
▲ chillily
5楼-- · 2019-02-04 02:05

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.

查看更多
成全新的幸福
6楼-- · 2019-02-04 02:07

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.

查看更多
萌系小妹纸
7楼-- · 2019-02-04 02:15

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();
}
查看更多
登录 后发表回答