wpf XAML 中 x:Key、x:Name、x:Type、x:Static 的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20846610/
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-13 10:20:24  来源:igfitidea点击:

Meaning of x:Key, x:Name, x:Type, x:Static in XAML

c#.netwpfxaml

提问by Yoda

On the msdn site there is big article: XAML overview

在 msdn 站点上有一篇大文章:XAML 概述

And there is the part describing what is: x:Key, x:Class, x:Nameetc. but the problem is that all that said there about it is very abstractional with no examples.

还有一部分描述了什么是:x:Key, x:Class, x:Name等等,但问题是关于它的所有内容都是非常抽象的,没有任何例子。

I know that when I create an element in xaml and set: x:Name = "abc"then in the cs file I have access to this object by abc.fieldORmethod()but what about the rest. Could anybody provide explanation with examples for that statements below?

我知道当我在 xaml 中创建一个元素并设置:x:Name = "abc"然后在 cs 文件中我可以访问这个对象,abc.fieldORmethod()但是其余的呢。有人可以为下面的陈述提供示例解释吗?

  • x:Key: Sets a unique key for each resource in a ResourceDictionary(or similar dictionary concepts in other frameworks). x:Key will probably account for 90% of the x: usages you will see in a typical WPF application's markup. x:Class: Specifies the CLR namespace and class name for the class that provides code-behind for a XAML page. You must have such a class to support code-behind per the WPF programming model, and therefore you almost always see x: mapped, even if there are no resources.
  • x:Name: Specifies a run-time object name for the instance that exists in run-time code after an object element is processed. In general, you will frequently use a WPF-defined equivalent property for x:Name. Such properties map specifically to a CLR backing property and are thus more convenient for application programming, where you frequently use run time code to find the named elements from initialized XAML. The most common such property is FrameworkElement.Name. You might still use x:Name when the equivalent WPF framework-level Name property is not supported in a particular type. This occurs in certain animation scenarios.
  • x:Static: Enables a reference that returns a static value that is not otherwise a XAML-compatible property.
  • x:Type: Constructs a Type reference based on a type name. This is used to specify attributes that take Type, such as Style.TargetType, although frequently the property has native string-to-Type conversion in such a way that the x:Type markup extension usage is optional.
  • x:Key:为a ResourceDictionary(或其他框架中的类似字典概念)中的每个资源设置唯一键。x:Key 可能占您将在典型 WPF 应用程序标记中看到的 x: 用法的 90%。x:Class: 指定为 XAML 页提供代码隐藏的类的 CLR 命名空间和类名。您必须有这样一个类来支持每个 WPF 编程模型的代码隐藏,因此您几乎总是看到 x: 映射,即使没有资源也是如此。
  • x:Name:为对象元素处理后存在于运行时代码中的实例指定运行时对象名称。通常,您会经常为 x:Name 使用 WPF 定义的等效属性。此类属性专门映射到 CLR 支持属性,因此更便于应用程序编程,在应用程序编程中,您经常使用运行时代码从初始化的 XAML 中查找命名元素。最常见的此类属性是 FrameworkElement.Name。当特定类型不支持等效的 WPF 框架级 Name 属性时,您可能仍会使用 x:Name。这发生在某些动画场景中。
  • x:Static:启用返回静态值的引用,否则该值不是 XAML 兼容的属性。
  • x:Type: 根据类型名称构造一个类型引用。这用于指定采用 Type 的属性,例如 Style.TargetType,尽管该属性通常具有本机字符串到类型的转换,因此 x:Type 标记扩展的使用是可选的。

回答by Rohit Vats

x:Keyis used in case you want to define some resource which can be reused in your xaml. It is equivalent to Key of normal dictionary.

x:Key用于定义一些可以在 xaml 中重用的资源。相当于普通字典的Key。

<Window.Resources>
   <Style x:Key="ButtonStyle"/>
</Window.Resources>

x:Staticis used to donate some static data. Suppose you want to declare brush with some static color defined under SystemColors enum.

x:Static用于捐赠一些静态数据。假设您想使用 SystemColors 枚举下定义的一些静态颜色来声明画笔。

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

x:Typeis equivalent to Typeclass in C#. It denotes type of class.

x:Type相当于TypeC# 中的类。它表示类的类型。

<Style TargetType="{x:Type Button}"/>

x:Nameis used to provide name to control so that it can be accessed from code behind using that name or can be binded within XAML using ElementName.

x:Name用于提供要控制的名称,以便可以使用该名称从后面的代码访问它,或者可以使用 ElementName 在 XAML 中绑定它。

<TextBlock x:Name="txt1" Text="Test"/>
<TextBlock x:Name="txt2" Text="{Binding Text,ElementName=txt}"/>