wpf - xaml 中的常量

标签 wpf xaml

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

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

我正在尝试在我的 xaml 中使用来自单独项目的这个常量。我包括了命名空间:
xmlns:constants="clr-namespace:MyProject.MyConstants;assembly=MyProject.MyConstants"

并尝试按如下方式使用常量:
<MenuItem Header="{x:Static controls:Constants.Group1Constants.DoIt}">

上面不会编译,说
Cannot find the type 'Constants.Group1Constants'. Note that type names are case sensitive.

我一定错过了一些简单的东西。我想要做的就是在我的 xaml 中使用不同项目中的类中的一些常量。

有什么建议么?

最佳答案

试试这个:

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

我用“+”代替“。”引用嵌套类。不确定这样做是否会遇到问题。

关于wpf - xaml 中的常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3919012/

相关文章:

c# - 优化渐变框动画

wpf - 从 XAML 设置 ViewModel 属性值

wpf - 在 WPF 的 XAML 中将系统颜色前景分配给 TextBlock

c# - 以下方法 : Identical. NameSpace.InitializeComponent() 和 Identical.NameSpace.InitializeComponent() 之间的调用不明确

c# - OxyPlot 的 LinearAxis 需要 margin

wpf - 如何重复使用相同的控件两次但保持它们之间的状态

c# - 在 MVVM 中使用 CommandParameter 传递类变量

c# - 为 Windows 应用商店中的 Combobox SelectedItem 实现双向绑定(bind)

c# - 如何提取 xaml/xml 文件中特定标签的所有出现?

wpf - 没有验证错误 WPF 时不显示工具提示