XAML中x:Key,x:Name,x:Type,x:Static的含义 [英] Meaning of x:Key, x:Name, x:Type, x:Static in XAML

查看:454
本文介绍了XAML中x:Key,x:Name,x:Type,x:Static的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On the msdn site there is big article: XAML overview

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

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

我知道当我在xaml中创建元素并设置时: x:Name = abc ,然后在cs文件中,我可以通过 abc.fieldORmethod()访问该对象关于其余的。 有人可以为下面的语句提供示例解释吗?

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?



  • x:Key :为 ResourceDictionary (或类似的字典概念)中的每个资源设置唯一的键其他
    框架)。 x:Key可能会占典型WPF应用程序标记中x:用法
    的90%。 x:Class
    指定
    为XAML页面提供代码隐藏的类的CLR命名空间和类名称。根据WPF编程模型,必须在
    后面提供此类支持代码的类,因此
    几乎总是会看到x:映射,即使没有资源也是如此。

  • x:Name :为对象元素处理后存在于运行时代码中的实例指定运行时对象名称。
    通常,您经常将WPF定义的等效
    属性用于x:Name。这样的属性专门映射到CLR
    的后备属性,因此对于应用程序
    编程更为方便,在该应用程序中,您经常使用运行时代码从初始化的XAML中查找
    命名元素。最常见的此类属性
    FrameworkElement.Name 。当
    特定类型不支持
    等效的WPF框架级Name属性时,您仍可以使用x:Name。在某些动画情况下会发生这种情况。

  • x:Static :启用返回静态值的引用

  • x:Type :构造基于类型的引用在类型名称上。该属性用于指定采用Type的属性,例如
    Style.TargetType ,尽管该属性通常在其中具有本机的
    字符串到Type转换。 x:Type标记
    扩展名用法是可选的。

  • 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 用于需要定义一些可在xaml中重用的资源的情况。

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

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

x:Static 用于捐赠一些静态数据。假设您要声明具有在SystemColors枚举下定义的某些静态颜色的画笔。

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

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

x:Type 等同于 Type C#中的类。

x:Type is equivalent to Type class in C#. It denotes type of class.

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

x:Name 用于提供名称来控制,以便可以使用该名称从后面的代码访问,也可以使用ElementName绑定到XAML中。

x:Name is 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.

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

这篇关于XAML中x:Key,x:Name,x:Type,x:Static的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆