Is there any difference in x:name and name for controls in xaml file?
I am new in Silverlight.
Wh开发者_如何学运维en I add some control to my xaml file with Visual Studio it set controls name with Name property, but there is also x:Name. Is there any difference and when to use each of them? Thanks.In Brief
Yes there is a difference. The bottom line is that x:Name
can be used on object elements that do not have Name
properties of their own.
A longer explanation
You can only use Name
on an element that represents an object that actually does have a Name
property. For example anything that derives from FrameworkElement
.
The x:Name
attribute may be placed on any element that represents an object regardless of whether that object actually has a Name
property. If the object does have a Name
property then the value of x:Name
will be assigned to it hence you can't have both x:Name
and Name
on the same element.
When an object has a Name
property or an x:Name
property the value of that property is associated with the objects entry in the object tree. It is via the object tree that the FindName
method of a FrameworkElement
can find an object. FindName
can find objects by name even if that object does not carry a Name
property of its own since it uses the name recorded in the object tree.
The autogenerated code for a UserControl
will contain field definitions for any element that that has a Name
or x:Name
property. The InitialiseComponent
method that is generated will use the FindName
method to assign values to these fields.
Example
The above Xaml creates two fields LayoutRoot
of type Grid
and MyBrush
of type SolidColorBrush
. If you were to change x:Name="LayoutRoot"
to Name="LayoutRoot"
that would change nothing. Grid
has a Name
property. However try changing x:Name="MyBrush"
to Name="MyBrush"
. That doesn't work because SolidColorBrush
doesn't have a name property. With the above Xaml you can then do code like this:-
public MainPage()
{
InitializeComponent();
MyBrush.Color = Colors.LightGray;
}
Open the definition of InitializeComponent
and take a look at the auto generated code.
No, you just can't use them both. x:Name is what the XAML preprocessor actually uses and Name is just a convience property provided on the FrameworkElement class to set it.
From the MSDN reference:
If Name is available as a property on an element, Name and x:Name can be used interchangeably, but an error results if both attributes are specified on the same element.
Short answer: if you're writing stuff out in XAML, it's probably best to just use x:Name consistently.
Long answer: A previous answer mentioned that Name is a "convienience" property for accessing x:Name. That's correct. However, now that the tools environment for XAML in both Visual Studio and the Expression series has really matured and you are seeing more and more tool-generated XAML, you are also probably seeing more and more x:Name as opposed to Name. The tools prefer x:Name because that way they don't take a somewhat risky dependency (potentially specific to framework) re: how x:Name and Name are really the same, and they don't need to flipflop between setting Name if something happens to be a FrameworkElement and then x:Name on something like a Storyboard and generating a duality if you were to look at this XAML through something like a DOM. In other words, the "Name" attribute in XAML really is a lot less "convenient" to use nowadays than might have been conceived of in the original API design. Part of the "convenience" was to not have to map x:, but you have to do that anyways for x:Class and by now pretty much everyone has gotten used to using x: attributes and the general principles of XAML markup effectively.
I'm not sure of the statement made by the original poster that VS encourages using Name. Yes, Name appears as an intellisense option, but so does x:Name. And all the cases I see in the templates where an object is given a starting name are using x:Name even tho most of these are FrameworkElements.
精彩评论