绑定到使用XAML添加到resx文件的图像的图像 [英] Images bound to images added to resx files using XAML

查看:614
本文介绍了绑定到使用XAML添加到resx文件的图像的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF应用程序包含一个资源文件MyResources.resx,其中包含多个字符串和图像。因为应用程序需要本地化,所以我对全局化资源的所有引用都必须通过自动生成的MyResources类的命名属性来完成。以下代码适用于字符串资源:

My WPF application includes a resource file MyResources.resx, containing several strings and images. Because the application will need to be localized, all my references to globalized resources must be made via named properties of the auto-generated MyResources class. The following code works well for string resources:

<Button Content="{x:Static local:Properties.MyResources.ButtonText}" />

但是这对图像不起作用。假设我有一个图像eflag.bmp作为名为Flag的资源添加到资源中,我希望能够做到这样的事情:

However the same does not work for images. Assuming I have an image eflag.bmp added to the resources as a resource named Flag, I would like to be able to do something like this:

<Image Source="{x:Static local:Properties.MyResources.Flag}" />

请注意以下替代方法:

<Image Source="/MyNamespace;component/Resources/eflag.bmp" />

在这种情况下无法使用,因为它无法处理本地化。使用后面的代码可以解决问题,但我正在寻找基于XAML的解决方案。

cannot be used in this case because it will not be able to handle localization. The problem can be solved using code behind but I am looking for a XAML based solution.

推荐答案

转动 x:静态 Binding.Source 并添加一个 转换器 Bitmap to ImageSource

Source="{Binding Source={x:Static local:Properties.MyResources.Flag},
                 Converter={StaticResource BitmapToImageSourceConverter}}"

或者您可以将转换器设为自定义标记扩展,它带有位图和返回 ImageSource ProvideValue

Alternatively you can make the converter a custom markup extension which takes a Bitmap and returns the ImageSource in ProvideValue.

Source="{me:BitmapToImageSource {x:Static local:Properties.MyResources.Flag}}"

这篇关于绑定到使用XAML添加到resx文件的图像的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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