WPF 应用程序中图像文件夹的 Uri

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

Uri for Images Folder in WPF Applications

c#wpfvisual-studio-2013uri

提问by JimmyK

My project looks like this:

我的项目是这样的:

enter image description here

在此处输入图片说明

Ideally I would like to have all my images in the 'Images' folder, however at the moment, I have a bunch of images in Visual Studio 2013\Projects\TestProject\TestProject\bin\Debug\Images. I had to do this because as I was trying to find a way to change the source of a ImageBrush in code, I ended up doing this:

理想情况下,我希望将所有图像都放在“图像”文件夹中,但是目前,我在 Visual Studio 2013\Projects\TestProject\TestProject\bin\Debug\Images 中有一堆图像。我不得不这样做,因为当我试图找到一种方法来更改代码中 ImageBrush 的来源时,我最终这样做了:

imagebrush.ImageSource = new BitmapImage(new Uri("Images/testimage.png", UriKind.Relative));

As much as I tried, I just couldn't find a way to point the Uri towards the Images folder in my first picture. Instead, I noticed the above code points towards the Debug folder of my project, hence why I ended up creating another 'Images' folder inside of there. In fact, I even found some tutorials that specifically say to do this when you want to change image sources in code.

尽我所能,我只是找不到一种方法将 Uri 指向我第一张照片中的 Images 文件夹。相反,我注意到上面的代码指向我项目的 Debug 文件夹,因此我最终在那里创建了另一个“Images”文件夹。事实上,我什至发现一些教程专门说当你想在代码中更改图像源时要这样做。

This seems wrong however... Especially now that I essentially have two different Image folders. Can someone please explain to me how I can get my code to point towards the Images folder in my picture?

然而,这似乎是错误的......特别是现在我基本上有两个不同的图像文件夹。有人可以向我解释如何让我的代码指向图片中的 Images 文件夹吗?

回答by JimmyK

I always had trouble with this, but thanks to Ganesh I finally found a solution:

我总是遇到这个问题,但多亏了 Ganesh,我终于找到了解决方案:

"pack://application:,,,/Images/testimage.png"

Pretty simple.

很简单。

回答by Quest

To access the 'Images' folder from code behind type like this

要从这样的代码隐藏类型访问“图像”文件夹

     string imageLocation = @"\Images\myImage.png";