The title says it all. Sometimes it seems that the Name
and x:Name
attributes are interchangeable.
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?
EDIT Responses so far suggest that using x:Name
all the time works fine, but I still want to know what the difference is. Microsoft put these two attributes into the very first release of WPF, so there must be some sensible explanation.
5 Answers
---------------------------------------------------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:Name by using the RuntimeNamePropertyAttribute on the class that designates one of the classes properties as mapping to the x:Name attribute of XAML.
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, FrameworkElement introduces a Name property.
In general, a class does not need to store the name for x:Name to be useable. All x:Name means 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.
So, why are there two ways to do the same thing? The simple answer because there are two concepts mapped onto one property. WPF wants the name of an element preserved at runtime (which is usuable 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 my marking the Name property as an alias of x:Name.
In the future, XAML will have more uses for x:Name, such as allowing you to set properties by refering to other objects by name, but in 3.5 and prior, it is only used to create fields.
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.
---------------------------------------------------
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.
Controls can expose name themselves as a DP if they want to (because they need to use that DP internally), or they can choose not to.
More details in msdn here and 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.
Comments:
----------------------------------------
If there's no difference, then why would there be two ways of doing the same thing? Both ways existed in the first release of WPF. – Drew Noakes Feb 26 at 11:15
----------------------------------------
Lol, did you down vote all the answers? – Steve Robbins Feb 26 at 11:28
----------------------------------------
@Steve, I didn't downvote any of the answers on this question, even though none of them so far have been very appropriate. – Drew Noakes Feb 26 at 11:53
----------------------------------------
I don't see how an answer that not only gives you the answer, but also gives you links to MSDN for a more information on the topic isn't appropriate? :-) – Steve Robbins Feb 26 at 11:55
----------------------------------------
@Steve your original answer did not address my question, hence my comment. I'm not looking for blind-faith "do it this way", but rather an insightful answer that explained why two ways exist, even if one of them does work all the time. Technically correct != Appropriate. Your update is much better. – Drew Noakes Feb 26 at 15:16
---------------------------------------------------
x:Name and Name are referencing different namespaces.
x:name is a reference to the x namespace defined by default at the top of the Xaml file.
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Just saying Name uses the default below namespace.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
x:Name is saying use the namespace that has the x alias. x is the default and most people leave it but you can change it to whatever you like
xmlns:foo="http://schemas.microsoft.com/winfx/2006/xaml"
so your reference would be foo:name
Define and Use Namespaces in WPF
EDIT:
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:name and name. All xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" and xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" are is references to multiple namespaces. Since xaml holds the Control namespace(not 100% on that) and presentation holds the FrameworkElement AND the Button class has a inheritance pattern of:
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. So you can say x:Name or Name and they will both be accessing the getter/setter from the FrameworkElement.
WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XML namespace. The XmlnsDefinitionAttribute attribute 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/presentation namespace.
So the assembly attributes will look something like:
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")]
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.
Comments:
----------------------------------------
If there's no difference, then why would there be two ways of doing the same thing? Both ways existed in the first release of WPF. – Drew Noakes Feb 26 at 11:13
----------------------------------------
---------------------------------------------------
It's not a WPF item but a standard XML one and BtBh
has 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 justName
is nothing more than a short hand forx:Name
. More details on XML namespaces can be found at link text
---------------------------------------------------
原文见 http://stackoverflow.com/questions/589874/in-wpf-what-are-the-differences-between-the-xname-and-name-attributes
转过来先 有空再翻