xaml 中的常量 [英] Constants in xaml

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

问题描述

假设我有一个类定义如下:

Let's say I have a class defined something like the below:

namespace MyProject.MyConstants
{
    public class Constants
    {
        public class Group1Constants
        {
            public const string DoIt= "DoIt";
        }
    }
}

我正在尝试在我的 xaml 中使用来自一个单独项目的这个常量.我包括了命名空间:

I am trying to use this const, from a separate project, in my xaml. I included the namespace:

xmlns:constants="clr-namespace:MyProject.MyConstants;assembly=MyProject.MyConstants"

并且我正在尝试按如下方式使用常量:

and am trying to use the constant as follows:

<MenuItem Header="{x:Static controls:Constants.Group1Constants.DoIt}">

以上不会编译,说

Cannot find the type 'Constants.Group1Constants'. Note that type names are case sensitive.

我一定遗漏了一些简单的东西.我想要做的就是在我的 xaml 中使用不同项目中某个类的一些常量.

I must be missing something simple. All I want to do is use some constants from a class in different project in my xaml.

有什么建议吗?

推荐答案

试试这个:

<MenuItem Header="{x:Static constants:Constants+Group1Constants.DoIt}">

我使用了+"而不是."引用嵌套类.不确定这样做是否会遇到问题.

I used "+" instead of "." to reference the nested class. Not sure if you'll run into problems doing this though.

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

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