如何获取存储在资源中的图像的 Uri [英] How to get a Uri of the image stored in the resources

查看:22
本文介绍了如何获取存储在资源中的图像的 Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 .png 文件添加到我的资源中,我需要在绑定时访问它们的 Uri.

I have two .png files added to my resources which I need to access their Uri when doing binding.

我的xaml代码如下:

<Grid>
  <Image>
    <Image.Source>
       <BitmapImage DecodePixelWidth="10" UriSource="{Binding Path=ImagePath}"/>
    </Image.Source>
  </Image> 
</Grid>

和使用 ImagePathbinding 代码是:

ImagePath = resultInBinary.StartsWith("1") ? Properties.Resources.LedGreen : Properties.Resources.ledRed;

不过

Properties.Resources.LedGreen

返回包含该特定图像的 Uri 的 Bitmap 而不是 String.我只想知道如何提取该值而无需在其存储的目录中寻址图像的路径.(老实说,我不确定这样做是否正确,因为我在网上找不到任何类似的情况).

returns a Bitmap instead of String containing the Uri of that particular image. I just want to know how to extract that value without a need to address a path of the image in the directory that it's stored. (Which honestly I am not sure is a right thing to do as I couldn't find any similar situation on the net).

如果有的话,请告诉我是否有比我正在尝试使用的方法更受欢迎的方法.

Please let me know if there is even a preferred method to the one I am trying to use if available.

推荐答案

在 WPF 应用程序中,您通常不会将图像存储在 Properties/Resources.resx 中并通过 访问它们Properties.Resources 类.

In a WPF application you would usually not store images in Properties/Resources.resx and access them by means of the Properties.Resources class.

相反,您只需将图像文件作为常规文件添加到 Visual Studio 项目中,可能位于名为Images"等的文件夹中.然后您将他们的 Build Action 设置为 Resource,这是在属性"窗口中完成的.你到那里例如通过右键单击图像文件并选择Properties 菜单项.请注意,无论如何,对于图像文件,Build Action 的默认值应该是 Resource.

Instead you just add the image files to your Visual Studio project as regular files, perhaps in a folder named "Images" or the like. Then you would set their Build Action to Resource, which is done in the Properties window. You get there e.g. by right-clicking the image file and select the Properties menu item. Note that the default value of the Build Action should be Resource for image files anyways.

为了从代码访问这些图像资源,您需要使用 Pack URI.使用上述文件夹名称Images"和名为LedGreen.png"的图像文件,创建这样的 URI 将如下所示:

In order to access these image resources from code you would then use a Pack URI. With the above folder name "Images" and an image file named "LedGreen.png", creating such an URI would look like this:

var uri = new Uri("pack://application:,,,/Images/LedGreen.png");

所以你或许可以将你的属性声明为 Uri 类型:

So you could perhaps declare your property to be of type Uri:

public Uri ImageUri { get; set; } // omitted INotifyPropertyChanged implementation

并像这样设置:

ImageUri = resultInBinary.StartsWith("1")
         ? new Uri("pack://application:,,,/Images/LedGreen.png")
         : new Uri("pack://application:,,,/Images/LedRed.png");

最后,您的 XAML 应如下所示,它依赖于从 Uri 到 ImageSource 的内置类型转换:

Finally your XAML should look like shown below, which relies on built-in type conversion from Uri to ImageSource:

<Grid>
    <Image Width="10" Source="{Binding Path=ImageUri}" />
</Grid>

这篇关于如何获取存储在资源中的图像的 Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆