.net “{x:Static}”在 XAML 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/507942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 11:09:48  来源:igfitidea点击:

What does "{x:Static}" mean in XAML?

.netwpfxaml

提问by Mark Cidade

What does {x:Static}mean in XAML?

{x:Static}在 XAML中是什么意思?

Code sample:

代码示例:

<SolidColorBrush Color="{x:Static SystemColors.ControlColor}" />

回答by Szymon Rozga

It is a way to insert any static value into XAML. For example, if I have a class:

这是一种将任何静态值插入到 XAML 中的方法。例如,如果我有一个类:

namespace A 
{ 
    public class MyConstants 
    {
        public static readonly string SomeConstantString = "BAM!";
    }
}

I can place it into a WPF UI using XAML like this:

我可以使用 XAML 将它放入 WPF UI 中,如下所示:

<TextBlock Text="{x:Static A:MyConstants.SomeConstantString}" />

Notice, you will have to import the namespace in which MyConstants is defined into your XAML. So in the or element do something like:

请注意,您必须将定义 MyConstants 的命名空间导入 XAML。所以在 or 元素中执行以下操作:

xmlns:A="clr-namespace:A"

回答by TcKs

From MSDN: http://msdn.microsoft.com/en-us/library/ms742135.aspx

来自 MSDN:http: //msdn.microsoft.com/en-us/library/ms742135.aspx

References any static by-value code entity defined in a Common Language Specification (CLS) compliant way The property referenced is evaluated prior to loading the remainder of the XAML page and can be used to provide the value of a property in XAML.

引用以符合公共语言规范 (CLS) 的方式定义的任何静态按值代码实体 引用的属性在加载 XAML 页面的其余部分之前进行评估,并可用于提供 XAML 中的属性值。

回答by Mark Cidade

I found the question XAML - Accessing static fieldshaving an answer which links to the MSDN documentation x:Static Markup Extension. I figured this would still be useful to have on the site.

我发现问题XAML - 访问静态字段有一个链接到 MSDN 文档x:Static Markup Extension的答案。我认为这在网站上仍然有用。