short info
Yes, there is a difference. The bottom line is that x:Name can be used for elements of an object that do not have their own Name properties.
Longer explanation
You can only use Name for an element that represents an object that actually has a Name property. For example, everything that comes from FrameworkElement .
The x:Name attribute can be placed on any element that represents an object, regardless of whether this object has a Name property. If the object has the Name property, then it is assigned the value x:Name , so you cannot have both x:Name and Name on the same element.
When an object has the Name property or the x:Name property, the value of this property is associated with the recording of objects in the object tree. Through the tree of objects, the FindName method of the FindName object can find the object. FindName can find objects by name, even if this object does not have its own Name property, since it uses the name recorded in the object tree.
The auto-generated code for UserControl will contain field definitions for any element that has the Name or x:Name property. The FindName InitialiseComponent method will use the FindName method to assign values ββto these fields.
Example
The above Xaml creates two LayoutRoot fields of type Grid and MyBrush type SolidColorBrush . If you were to change x:Name="LayoutRoot" to Name="LayoutRoot" , this will not change anything. Grid has a Name property. However, try changing x:Name="MyBrush" to Name="MyBrush" . This does not work because SolidColorBrush does not have a name property. With the above Xaml, you can make the code as follows: -
public MainPage() { InitializeComponent(); MyBrush.Color = Colors.LightGray; }
Open the InitializeComponent definition and take a look at the automatically generated code.
AnthonyWJones
source share