WPF XAML 中的 My.Resources? [英] My.Resources in WPF XAML?

查看:34
本文介绍了WPF XAML 中的 My.Resources?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过 Xaml 访问 My.Resources?

Is there a way to access My.Resources thru Xaml?

像这样

<Image Source="{Binding MyImage,
    Source={x:Static my:Resources},
    Converter={StaticResource MyBitmapToImageSourceConverter}}" />

这是我得到的错误:\Application.xaml(122,36):错误 MC3029:my:Resources"成员无效,因为它没有合格的类型名称.

以上当然行不通.

注意:转换器仅用于说明.

NOTE: The converter is for illustration only.

我想到了一个想法,如果可行的话,这可能是一个好方法,我为资源创建了一个包装类:

I thought about 1 idea, which might be a good approach if it will work, I created a wrapper class for the resources:

Public Class Resources
    Public Shared ReadOnly m_Resources As New Resources
    Public Shared ReadOnly Property Resources() As Resources
        Get
            Return m_Resources
        End Get
    End Property

    Public ReadOnly Property MyResources(ByVal name As String) As Object
        Get
            Return My.Resources.ResourceManager.GetObject(name)
        End Get
    End Property
End Class

然后在我的绑定中,我尝试像这样访问它:

And then in my binding I try to access it like so:

<Setter Property="ImageSource"
Value="{Binding MyResources[Ok], Source={x:Static src:Resources.Resources}}"/>

但我仍然收到以下消息:

But I still get the following message:

System.Windows.Data 错误:16:无法从 ''(类型'Resources')获取'MyResources' 值(类型'Object').BindingExpression:Path=MyResources[OK];数据项='资源'(哈希码=59109011);目标元素是 'Image' (Name='btnOk');目标属性是源"(类型图像源")TargetParameterCountException:System.Reflection.TargetParameterCountException:参数计数不匹配.

顺便说一句,我在 MyResources Getter 中放置了一个测试 MessageBox,似乎根本没有访问该属性.

BTW, I placed a test MessageBox in the MyResources Getter, and it seems the property is not accessed at all.

推荐答案

问题是,默认情况下,为 Resources.resx 文件生成代码的工具是 VbMyResourcesResXFileCodeGenerator(自定义工具"属性项目项).这个工具会生成一个模块,其中的资源属性是内部的(Friend),所以StaticExtension无法访问它.要解决这个问题,您应该将 Resources.resx 的自定义工具更改为 PublicVbMyResourcesResXFileCodeGenerator,这将生成公共成员.

The problem is that by default, the tool that generates code for the Resources.resx file is VbMyResourcesResXFileCodeGenerator ("Custom tool" property of the project item). This tool generates a Module in which the resource properties are internal (Friend), so the StaticExtension can't access it. To solve that problem, you should change the custom tool for Resources.resx to PublicVbMyResourcesResXFileCodeGenerator, which will generate public members.

此外,VB 模块大致相当于静态(共享)类,因此没有可以用作绑定源的 Resources 实例,因此您无法为绑定指定 Path.您应该将绑定源直接设置为您想要的属性:

Also, a VB module is roughly equivalent to a static (Shared) class, so there is no instance of Resources that could be used as the source of the binding, so you can't specify a Path for the binding. You should set the binding source directly to the property you want :

<Image Source="{Binding Source={x:Static my:Resources.MyImage},
    Converter={StaticResource MyBitmapToImageSourceConverter}}" />

注意:还有一对工具可用于生成资源文件的代码:ResXFileCodeGeneratorPublicResXFileCodeGenerator.这些工具生成一个类而不是一个模块.

Note: there is another pair of tools available to generate the code for a resource file : ResXFileCodeGenerator and PublicResXFileCodeGenerator. These tools generate a class instead of a module.

要使用的命名空间映射如下:

The namespace mapping to use is the following :

xmlns:myRes="clr-namespace:YourApplicationName.My.Resources"

这篇关于WPF XAML 中的 My.Resources?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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