Constants in xaml

2020-07-03 03:58发布

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";
        }
    }
}

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}">

The above wont compile though, saying that

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

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

Any suggestions?

标签: wpf xaml
1条回答
够拽才男人
2楼-- · 2020-07-03 04:08

Try this one:

<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.

查看更多
登录 后发表回答