WPF 中的图像源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24432407/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:00:37  来源:igfitidea点击:

Image Source in WPF

c#wpfxaml

提问by Ryan

I'm trying to set an image source in XAML but keep getting "Could not find part of a path..." (plus the directory I want to call). I think I'm messing up what location I'm supposed to be calling. The hierarchy looks something like:

我正在尝试在 XAML 中设置图像源,但不断收到“找不到路径的一部分...”(加上我想调用的目录)。我想我搞砸了我应该打电话的地方。层次结构类似于:

-Solution
   -Project
      -Data
      -Images
         -Image_I_want_to_use.png (placeholder name)
      -Themes
         -Demo
           -Default
              -fileImWorkingIn.xaml
              -other files
      -other folders
   -Another Project
   -Third Project

How would I configure my image source in XAML so the file I'm working in can utilize the image(s)?

我将如何在 XAML 中配置我的图像源,以便我正在处理的文件可以使用图像?

I tried

我试过

<Image Source="/Project;component/Images/image_to_use.png">

(where each name is simply a placeholder) but had no luck. Any pointers? Apologies if this is trivial.

(其中每个名称只是一个占位符)但没有运气。任何指针?如果这是微不足道的,请道歉。

Thanks!

谢谢!

回答by Sheridan

In .NET 4, this Image.Sourcevalue would work:

在 .NET 4 中,这个Image.Source值会起作用:

<Image Source="/AssemblyName;component/FolderName/image_to_use.png">

However, Microsoft made some horrible changes in .NET 4.5 that broke many different things and so in .NET 4.5, you'd need to use the full packpath like this:

然而,微软在 .NET 4.5 中做了一些可怕的改变,破坏了许多不同的东西,所以在 .NET 4.5 中,你需要使用这样的完整pack路径:

<Image Source="pack://application:,,,/AssemblyName;component/Images/image_to_use.png">

回答by Ryan

What fixed it:

什么修复了它:

<Image Source ="pack://application:,,,/Images/image_name.png"></Image>

I also changed the Build Action to content.

我还将构建操作更改为内容。

Thanks for your help everyone!

谢谢大家的帮助!