I am new in Silverlight.
When 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.
相关问题
- WPF Binding from System.Windows.SystemParameters.P
- Xamarin. The name 'authorEntry does not exist
- how to Enumerate local fonts in silverlight 4
- XAML: Applying styles to nested controls
- How to properly change a resource dictionary
相关文章
- WPF MVVMLight:在ViewModel中,开子线程为ObservableCollectio
- 关于ItemsControl的绑定问题
- XAML ComboBox SelectionChanged Fires OnLoad
- Show flyout using BottomAppBar
- Why does my ScrollViewer destroy my Grid Layout? W
- Can I add/subtract value that is bound to an eleme
- How to overlay items in StackPanel or ListView?
- C# UWP Toolkit DropShadowPanel inner shadow
In Brief
Yes there is a difference. The bottom line is that
x:Name
can be used on object elements that do not haveName
properties of their own.A longer explanation
You can only use
Name
on an element that represents an object that actually does have aName
property. For example anything that derives fromFrameworkElement
.The
x:Name
attribute may be placed on any element that represents an object regardless of whether that object actually has aName
property. If the object does have aName
property then the value ofx:Name
will be assigned to it hence you can't have bothx:Name
andName
on the same element.When an object has a
Name
property or anx:Name
property the value of that property is associated with the objects entry in the object tree. It is via the object tree that theFindName
method of aFrameworkElement
can find an object.FindName
can find objects by name even if that object does not carry aName
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 aName
orx:Name
property. TheInitialiseComponent
method that is generated will use theFindName
method to assign values to these fields.Example
The above Xaml creates two fields
LayoutRoot
of typeGrid
andMyBrush
of typeSolidColorBrush
. If you were to changex:Name="LayoutRoot"
toName="LayoutRoot"
that would change nothing.Grid
has aName
property. However try changingx:Name="MyBrush"
toName="MyBrush"
. That doesn't work becauseSolidColorBrush
doesn't have a name property. With the above Xaml you can then do code like this:-Open the definition of
InitializeComponent
and take a look at the auto generated code.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.
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.