Xamarin XAML x:对嵌套类中的属性的静态引用 [英] Xamarin XAML x:Static reference to property in a nested class

查看:81
本文介绍了Xamarin XAML x:对嵌套类中的属性的静态引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xamarin编写移动应用程序,并且我有一个名为Strings的静态类,用于包装我的RESX资源.我想使用x:Static绑定到我的XAML文件中.如果我有一个要绑定到静态属性的静态类,则此方法有效.

I'm writing a mobile app using Xamarin and I have a static class called Strings that wraps my RESX resources. I want to use x:Static to bind to these in my XAML files. This is working if I have a single static class with static properties to bind to.

我要删除一些注释和其他不必要的内容,但基本上看起来像这样:

I'm cutting out some comments and other non-essential bits, but it basically looks like this:

namespace MyCompany.Resources
{
    public static partial class Strings
    {
        public static string LabelUsername { get { return Resources.LabelUsername; } }
    }
}

然后在我的XAML中,我将其绑定为:

Then in my XAML, I bind to it like this:

<条目占位符="{x:静态资源:Strings.LabelUsername}"

其中资源定义为

xmlns:resources ="clr-namespace:MyCompany.Resources; assembly = MyCompany"

一切正常.当我向字符串添加嵌套类时,它崩溃了.该类如下所示:

That all works fine. It breaks down when I add a nested class to Strings. The class looks like this:

namespace MyCompany.Resources
{
    public static partial class Strings
    {
        public static partial class Label
        {
            public static string Username { get { return Resources.Label_Username; } }
        }
    }
}

如果我这样做,那么我将在XAML中将其绑定为:

If I do that, then I would bind to it in my XAML like this:

<Entry Placeholder="{x:Static resources:Strings.Label.Username}"

注意在"resources:"之后的方式,我们现在具有三个级别(Strings.Label.Username).这似乎是失败的原因.当我这样做时,我得到了编译错误:在xmlns clr-namespace:MyCompany.Resources; assembly = MyCompany 中找不到类型Strings.Label

Notice how after "resources:" we now have three levels (Strings.Label.Username). This seems to be what fails. When I do this, I get the compile error: Type Strings.Label not found in xmlns clr-namespace:MyCompany.Resources;assembly=MyCompany

此外,我还可以从ViewModels中访问嵌套类及其属性.有什么方法可以通过XAML进行这项工作吗?我知道我可以绑定到VM中的变量,然后使用该引用Strings.Label.Username,但是我不想为每个资源绑定都这样做.

Also, I can access the nested class and its properties just fine from my ViewModels. Is there any way to make this work from the XAML? I know I could bind to a variable in the VM and then have that reference Strings.Label.Username, but I don't want to do that for every resource binding.

推荐答案

绑定中的静态属性名称应为

Your static property's name in the binding should be

Strings+LabelUsername.Username

不仅有错字,而且还尝试使用点表示法来引用嵌套类,但这种方法不起作用.

Not only did you have a typo, but you tried to use the dot notation to reference the nested class, which won't work.

绑定使用标准的.net反射符号来按名称引用属性和类(它们要么在字符串上使用解析器,要么直接使用反射,因此无法检查代码库).嵌套的类名使用 + 分隔包含的类名和内部类名.您可以在这里了解更多有关此的信息:

Bindings use standard .net reflection notation for referencing properties and classes by name (they either use a parser on the string or use reflection directly, can't be arsed to check the codebase). Nested class names use a + to separate the containing class name and the inner class name. You can read more about that here:

C#:具有"+"号在课程名称中?

这篇关于Xamarin XAML x:对嵌套类中的属性的静态引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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