What does “{x:Static}” mean in XAML?

2019-01-09 00:46发布

What does {x:Static} mean in XAML?

Code sample:

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

标签: .net wpf xaml
3条回答
你好瞎i
2楼-- · 2019-01-09 01:16

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

查看更多
男人必须洒脱
3楼-- · 2019-01-09 01:16

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

查看更多
萌系小妹纸
4楼-- · 2019-01-09 01:19

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

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

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

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

xmlns:A="clr-namespace:A"
查看更多
登录 后发表回答