.net 在 WPF 中,x:Name 和 Name 属性之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/589874/
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
In WPF, what are the differences between the x:Name and Name attributes?
提问by Drew Noakes
The title says it all. Sometimes it seems that the Nameand x:Nameattributes are interchangeable.
标题说明了一切。有时似乎Name和x:Name属性可以互换。
So, what are the definitive differences between them, and when is it preferable to use one over the other?
那么,它们之间的明确区别是什么,什么时候使用一个比另一个更可取?
Are there any performance or memory implications to using them the wrong way?
以错误的方式使用它们是否有任何性能或内存影响?
采纳答案by chuckj
There really is only one name in XAML, the x:Name. A framework, such as WPF, can optionally map one of its properties to XAML's x:Nameby using the RuntimeNamePropertyAttributeon the class that designates one of the classes properties as mapping to the x:Name attribute of XAML.
XAML 中确实只有一个名称,即x:Name. 框架(例如 WPF)可以选择将其属性之一映射到 XAML x:Name,方法是使用RuntimeNamePropertyAttribute类上的 将类属性之一指定为映射到 XAML 的 x:Name 属性。
The reason this was done was to allow for frameworks that already have a concept of "Name" at runtime, such as WPF. In WPF, for example, FrameworkElementintroduces a Name property.
这样做的原因是允许在运行时已经具有“名称”概念的框架,例如 WPF。例如,在 WPF 中,FrameworkElement引入了 Name 属性。
In general, a class does not need to store the name for x:Nameto be useable. All x:Namemeans to XAML is generate a field to store the value in the code behind class. What the runtime does with that mapping is framework dependent.
通常,类不需要存储名称x:Name即可使用。x:NameXAML 的所有方法是生成一个字段来存储类背后的代码中的值。运行时对该映射执行的操作取决于框架。
So, why are there two ways to do the same thing? The simple answer is because there are two concepts mapped onto one property. WPF wants the name of an element preserved at runtime (which is usable through Bind, among other things) and XAML needs to know what elements you want to be accessible by fields in the code behind class. WPF ties these two together by marking the Name property as an alias of x:Name.
那么,为什么有两种方法可以做同样的事情呢?简单的答案是因为有两个概念映射到一个属性上。WPF 希望在运行时保留元素的名称(可通过 Bind 等使用),而 XAML 需要知道您希望类背后的代码中的字段可以访问哪些元素。WPF 通过将 Name 属性标记为 x:Name 的别名将这两者联系在一起。
In the future, XAML will have more uses for x:Name, such as allowing you to set properties by referring to other objects by name, but in 3.5 and prior, it is only used to create fields.
将来,XAML 将有更多 x:Name 的用途,例如允许您通过名称引用其他对象来设置属性,但在 3.5 及更早版本中,它仅用于创建字段。
Whether you should use one or the other is really a style question, not a technical one. I will leave that to others for a recommendation.
您是否应该使用一个或另一个实际上是一个风格问题,而不是技术问题。我会把它留给其他人推荐。
See also AutomationProperties.Name VS x:Name, AutomationProperties.Name is used by accessibility tools and some testing tools.
另见AutomationProperties.Name VS x:Name,AutomationProperties.Name 被辅助工具和一些测试工具使用。
回答by Kenan E. K.
They are not the same thing.
它们不是同一件事。
x:Nameis a xaml concept, used mainly to reference elements. When you give an element the x:Name xaml attribute, "the specified x:Namebecomes the name of a field that is created in the underlying code when xaml is processed, and that field holds a reference to the object." (MSDN) So, it's a designer-generated field, which has internal access by default.
x:Name是一个xaml概念,主要用于引用元素。当您为元素提供 x:Name xaml 属性时,“指定的x:Name将成为处理 xaml 时在底层代码中创建的字段的名称,并且该字段保存对对象的引用。” ( MSDN) 所以,它是一个设计器生成的字段,默认情况下具有内部访问权限。
Nameis the existing string property of a FrameworkElement, listed as any other wpf element property in the form of a xaml attribute.
Name是 a 的现有字符串属性FrameworkElement,以 xaml 属性的形式列为任何其他 wpf 元素属性。
As a consequence, this also means x:Namecan be used on a wider range of objects. This is a technique to enable anything in xaml to be referenced by a given name.
因此,这也意味着x:Name可以用于更广泛的对象。这是一种使 xaml 中的任何内容都能够被给定名称引用的技术。
回答by cgreeno
x:Name and Name are referencing different namespaces.
x:Name 和 Name 引用不同的命名空间。
x:nameis a reference to the x namespace defined by default at the top of the Xaml file.
x:name是对 Xaml 文件顶部默认定义的 x 命名空间的引用。
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Just saying Nameuses the default below namespace.
只是说Name使用下面的默认命名空间。
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
x:Nameis saying use the namespace that has the xalias. x is the default and most people leave it but you can change it to whatever you like
x:Name是说使用具有x别名的命名空间。x 是默认值,大多数人都保留它,但您可以将其更改为您喜欢的任何内容
xmlns:foo="http://schemas.microsoft.com/winfx/2006/xaml"
so your reference would be foo:name
所以你的参考是foo:name
Define and Use Namespaces in WPF
OK lets look at this a different way. Say you drag and drop an button onto your Xaml page. You can reference this 2 ways x:nameand name. All xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"andxmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"are is references to multiple namespaces. Since xamlholds the Controlnamespace(not 100% on that) and presentationholds the FrameworkElementAND the Button classhas a inheritance pattern of:
好的,让我们换一种方式来看待这个问题。假设您将一个按钮拖放到 Xaml 页面上。您可以参考这两种方式x:name和name。所有xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"和xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"都是对多个命名空间的引用. 由于xaml拥有Control命名空间(不是 100%),而presentation拥有FrameworkElement并且Button 类具有以下继承模式:
Button : ButtonBase
ButtonBase : ContentControl, ICommandSource
ContentControl : Control, IAddChild
Control : FrameworkElement
FrameworkElement : UIElement, IFrameworkInputElement,
IInputElement, ISupportInitialize, IHaveResources
So as one would expect anything that inherits from FrameworkElement would have access to all its public attributes. so in the case of Button it is getting its Name attribute from FrameworkElement, at the very top of the hierarchy tree. Soyou can say x:Nameor Nameand they will both be accessing the getter/setter from the FrameworkElement.
因此,正如人们所期望的那样,从 FrameworkElement 继承的任何内容都可以访问其所有公共属性。因此,在 Button 的情况下,它是从 FrameworkElement 获取其 Name 属性,位于层次结构树的最顶部。 因此,您可以说x:Name或Name,它们都将从 FrameworkElement 访问 getter/setter。
WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XML namespace. The XmlnsDefinitionAttributeattribute is placed at the assembly level in the source code that produces the assembly. The WPF assembly source code uses this attribute to map the various common namespaces, such as System.Windows and System.Windows.Controls, to the http://schemas.microsoft.com/winfx/2006/xaml/presentationnamespace.
WPF 定义了 XAML 处理器使用的 CLR 属性,以便将多个 CLR 命名空间映射到单个 XML 命名空间。所述XmlnsDefinitionAttribute属性被放置在产生该组件的源代码的程序集的水平。WPF 程序集源代码使用此属性将各种常见命名空间(例如 System.Windows 和 System.Windows.Controls)映射到http://schemas.microsoft.com/winfx/2006/xaml/presentation命名空间。
So the assembly attributes will look something like:
因此,程序集属性将类似于:
PresentationFramework.dll - XmlnsDefinitionAttribute:
PresentationFramework.dll - XmlnsDefinitionAttribute:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Data")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Navigation")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Shapes")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Documents")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls")]
回答by Steven Robbins
They're both the same thing, a lot of framework elements expose a name property themselves, but for those that don't you can use x:name - I usually just stick with x:name because it works for everything.
它们都是一样的东西,很多框架元素本身公开了一个 name 属性,但对于那些没有的你可以使用 x:name - 我通常只坚持使用 x:name 因为它适用于所有事情。
Controls can expose name themselves as a Dependency Property if they want to (because they need to use that Dependency Property internally), or they can choose not to.
如果控件愿意,它们可以将自己的名称公开为依赖属性(因为它们需要在内部使用该依赖属性),或者它们可以选择不使用。
More details in msdn hereand here:
Some WPF framework-level applications might be able to avoid any use of the x:Name attribute, because the Name dependency property as specified within the WPF namespace for several of the important base classes such as FrameworkElement/FrameworkContentElement satisfies this same purpose. There are still some common XAML and framework scenarios where code access to an element with no Name property is necessary, most notably in certain animation and storyboard support classes. For instance, you should specify x:Name on timelines and transforms created in XAML, if you intend to reference them from code.
If Name is available as a property on the class, Name and x:Name can be used interchangeably as attributes, but an error will result if both are specified on the same element.
某些 WPF 框架级应用程序可能能够避免使用 x:Name 属性,因为在 WPF 命名空间中为几个重要的基类(例如 FrameworkElement/FrameworkContentElement)指定的 Name 依赖属性满足了同样的目的。仍然有一些常见的 XAML 和框架方案需要对没有 Name 属性的元素进行代码访问,尤其是在某些动画和故事板支持类中。例如,如果您打算从代码中引用它们,您应该在时间轴和在 XAML 中创建的转换上指定 x:Name。
如果 Name 可用作类的属性,则 Name 和 x:Name 可以作为属性互换使用,但如果在同一元素上指定两者,则会导致错误。
回答by scott
X:Name can cause memory issues if you have custom controls. It will keep a memory location for the NameScope entry.
如果您有自定义控件,X:Name 可能会导致内存问题。它将为 NameScope 条目保留一个内存位置。
I say never use x:Name unless you have to.
我说除非必须,否则永远不要使用 x:Name。
回答by Oleksandr Zolotarov
Name:
姓名:
- can be used only for descendants of FrameworkElement and FrameworkContentElement;
- can be set from code-behind via SetValue() and property-like.
- 只能用于 FrameworkElement 和 FrameworkContentElement 的后代;
- 可以通过 SetValue() 和类似属性从代码隐藏中设置。
x:Name:
x:姓名:
- can be used for almost all XAML elements;
- can NOT be set from code-behind via SetValue(); it can only be set using attribute syntax on objects because it is a directive.
- 可用于几乎所有 XAML 元素;
- 不能通过 SetValue() 从代码隐藏设置;它只能使用对象的属性语法设置,因为它是一个指令。
Using both directives in XAML for one FrameworkElement or FrameworkContentElement will cause an exception: if the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load.
在 XAML 中为一个 FrameworkElement 或 FrameworkContentElement 使用这两个指令将导致异常:如果 XAML 是标记编译的,则异常将在标记编译时发生,否则在加载时发生。
回答by itzmebibin
x:Namemeans: create a field in the code behind to hold a reference to this object.
x:Name意思是:在后面的代码中创建一个字段来保存对这个对象的引用。
Namemeans: set the name property of this object.
Name意思是:设置这个对象的名称属性。
回答by Bipul Kumar
The only difference is that if you are using user Controls into a control from Same Assembly then Name will not identify your control and you will get an error " Use x:Name for controls in the same Assembly". So x:Name is the WPF versioning of naming controls in WPF. Name is just used as a Winform Legacy. They wanted to differentiate the naming of controls in WPF and winforms as they use attributes in Xaml to identify controls from other assemblies they used x: for Names of control.
唯一的区别是,如果您将用户控件用于同一程序集中的控件,则 Name 将无法识别您的控件,并且您将收到错误消息“将 x:Name 用于同一程序集中的控件”。所以 x:Name 是 WPF 中命名控件的 WPF 版本。Name 仅用作 Winform Legacy。他们希望区分 WPF 和 winform 中控件的命名,因为他们使用 Xaml 中的属性来从其他程序集中识别控件,他们使用 x: 作为控件名称。
Just keep in mind dont put a name for a control just for the sake of keeping it as it resides in memory as a blank and it will give you a warning that Name has been applied for a control buts its never used.
请记住,不要仅仅为了保持它作为空白驻留在内存中而为控件命名,它会警告您名称已应用于控件但从未使用过。
回答by Simon
I always use the x:Name variant. I have no idea if this affects any performance, I just find it easier for the following reason. If you have your own usercontrols that reside in another assembly just the "Name" property won't always suffice. This makes it easier to just stick too the x:Name property.
我总是使用 x:Name 变体。我不知道这是否会影响任何性能,我只是觉得它更容易,原因如下。如果您有自己的用户控件驻留在另一个程序集中,则“名称”属性并不总是足够的。这样可以更轻松地粘贴 x:Name 属性。
回答by Robert MacLean
It's not a WPF item but a standard XML one and BtBhhas correctly answered it, x refers to the default namespace. In XML when you do not prefix an element/attribute with a namespace it assumes you want the default namespace.
So typing just Nameis nothing more than a short hand for x:Name. More details on XML namespaces can be found at link text
它不是 WPF 项目,而是标准 XML 项目,BtBh正确回答了它,x 指的是默认命名空间。在 XML 中,当您不使用命名空间作为元素/属性的前缀时,它假定您需要默认命名空间。因此,打字只不过Name是x:Name. 可以在链接文本中找到有关 XML 名称空间的更多详细信息

