如何使用Resources.resx链接图像 [英] How to use Resources.resx to link images

查看:157
本文介绍了如何使用Resources.resx链接图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的Resources.resx中添加了一个图标文件,该文件要显示在堆栈面板内部的TreeViewItem上。

I have included an icon file inside my Resources.resx that I'd like to show on a TreeViewItem that is inside a stackpanel.

1)Can .ico文件用于此目的?还是必须是.bmp或jpg?

1)Can .ico files be used for this purpose? Or does it have to be .bmp or jpg?

2)您如何将源设置为XAML?以下代码对我不起作用

2)What do you set the source as in XAML? The following code didn't work for me

<StackPanel Orientation="Horizontal">
    <Image Margin="2" Source="/Resources/Folder_Back.ico" />
    <TextBlock Margin="2" Text="{Binding ProgramName}"
     Foreground="White" FontWeight="Bold"/>
</StackPanel>


推荐答案

您不能这样做。只能在winforms中工作

you can't do that. that worked only in winforms

有关更多信息,请参见此帖子

see this post for more info

将图像添加到资源的方式不同

使用

WPF图片资源

WPF image resources

相反

引用:

如果要在多个位置使用图像,则仅将图像数据加载到内存中一次,然后在所有 Image 元素之间共享是值得的。

If you will use the image in multiple places, then it's worth loading the image data only once into memory and then sharing it between all Image elements.

为此,创建一个 BitmapSource 作为某处的资源:

To do this, create a BitmapSource as a resource somewhere:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />

然后,在您的代码中使用类似以下内容的东西:

Then, in your code, use something like:

<Image Source="{StaticResource MyImageSource}" />

就我而言,我发现必须设置 Image.png 文件以具有资源的生成操作,而不仅仅是 Content 的生成操作。这会导致图像在已编译的程序集中携带。

In my case, I found that I had to set the Image.png file to have a build action of Resource rather than just Content. This causes the image to be carried within your compiled assembly.

这篇关于如何使用Resources.resx链接图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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