Wpf - imagem 'não faz parte do projeto ou sua ação de construção não está definida como recurso'

StackOverflow https://stackoverflow.com/questions/638845

Pergunta

Eu tenho um projeto que requer uma imagem na janela. Esta é uma imagem estática e eu adicionei através de 'Adicionar> Item existente'. Existe na raiz do projeto.

Eu faço referência à imagem em uma página de teste como 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>

O problema é que recebo uma mensagem dizendo que não pode ser encontrada ou sua ação de construção não é recurso, mas existe e sua ação de construção é um recurso. Se eu criar um novo aplicativo e basta jogá -lo em uma janela, ele funciona bem.

Qualquer ajuda seria ótimo.

Foi útil?

Solução

Tente fazer uma reconstrução completa ou exclua os arquivos de construção e crie o arquivo.

O Visual Studio nem sempre obtém mudanças nos recursos, e pode ser uma dor de que o recompile.

Tente também usar um URI completo, pois isso me ajudou quando tive o mesmo problema. Algo como

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

Outras dicas

→ Clique com o botão direito do mouse no arquivo de imagem → Clique em Propriedade → Selecione Ação de construção para Recurso → Limpe e construir solução → Executar a solução

Você vai conseguir tudo.

Eu tive o mesmo problema. Limpar e reconstruir a solução não a corrigi, então reiniciei o Visual Studio e o fez. Esperamos que o Visual 2010 corrija esse problema e os muitos outros que prendem o WPF no Visual 2008.

Tente iniciar o caminho para a sua imagem com um "/":

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

Existe uma solução para sua pergunta

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

"Componente" não é uma pasta!

Não, ou pelo menos o beta atual não. Encontrei esta página enquanto procurava exatamente o mesmo problema. Reconstruir/limpar nada fez nada. Depois de fechar e recarregar a solução, o arquivo se tornou magicamente compatível novamente.

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();
}

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

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.

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top