无法解析StaticExtension值 [英] StaticExtension value cannot be resolved

查看:191
本文介绍了无法解析StaticExtension值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将WPF绑定到静态属性时遇到了一些问题。在我的应用程序中,有一个名为 Globals 的类,其中包含 ThisAddIn static 属性。这个对象允许我访问一个User属性,该属性基本上就是我想在Binding中使用的属性。因此,我的XAML如下所示:

I'm facing some issues with a WPF binding to a static property. In my application there is a class called Globals which contains a static property to ThisAddIn. This object allows me to access a User property which basically is what I want to use in my Binding. So, my XAML looks like this:

<TextBlock Text="{Binding Path=User.Name
                   , Source={x:Static Member=s:Globals.ThisAddIn}}" />

名称空间 s 在顶部由以下行声明:

The namespace s is declared at the top by the following line:

xmlns:s="clr-namespace:ConsoleApplication16.Model"

我发现了很多不同的方法来引用XAML中的静态属性,但是除此方法外,其他方法均无效,这似乎也是最合逻辑的我。我发现的一些示例根本没有使用Path或Member。

I found a ton of different approaches of how to refer to the static Property in XAML, but none of them worked except for this one, which also seems the most logical for me. Some samples I found, did not use Path or Member at all.

经过一些测试,我发现正确的方法是使用 x:Static Member定义要使用的类和静态属性,而Path在对象内定义正确的属性,就像普通绑定一样。

After some testing I found the right way to do this, is using "x:Static Member" to define the class and the static property you want to use, while Path defines the correct Properties inside this object, just like a normal binding would do.

尽管此解决方案在编译时没有任何抱怨,但会弹出XAMLParseException,直接告诉我 StaticExtension值无法解析为枚举,静态字段或静态属性。

Although this solution compiles without any complains, a XAMLParseException pops up directly telling me that the StaticExtension value cannot be resolved to an enumeration, static field, or static property.

推荐答案

确保 Globals.ThisAddIn 是公开的。

如果您使用的是资源文件,也可能会遇到这种情况,在这种情况下,还需要确保将访问修饰符设置为 Public

You may also get this if you are using a resource file in which case you also need to ensure the access modifier is set to Public:

这篇关于无法解析StaticExtension值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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