xaml 文件中控件的 x:name 和 name 有什么区别吗? [英] Is there any difference in x:name and name for controls in xaml file?
问题描述
我是 Silverlight 的新手.
当我使用 Visual Studio 向我的 xaml 文件添加一些控件时,它会使用 Name 属性设置控件名称,但还有 x:Name.
有什么区别以及何时使用它们吗?
谢谢.
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.
推荐答案
简介
是的,有区别.最重要的是,x:Name
可用于自身没有 Name
属性的对象元素.
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.
更长的解释
您只能在表示实际具有 Name
属性的对象的元素上使用 Name
.例如从 FrameworkElement
派生的任何东西.
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
.
x:Name
属性可以放置在任何表示对象的元素上不管该对象是否实际上具有 Name
属性.如果对象确实具有 Name
属性,则 x:Name
的值将分配给它,因此您不能同时拥有 x:Name
> 和 Name
在同一个元素上.
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.
当对象具有 Name
属性或 x:Name
属性时,该属性的值与对象树中的对象条目相关联.FrameworkElement
的 FindName
方法可以通过对象树找到对象.FindName
可以通过名称查找对象,即使该对象不携带自己的 Name
属性,因为它使用对象树中记录的名称.
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.
UserControl
的自动生成代码将包含任何具有 Name
或 x:Name
属性的元素的字段定义.生成的 InitialiseComponent
方法将使用 FindName
方法为这些字段赋值.
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.
示例
上述 Xaml 创建了 Grid
类型的两个字段 LayoutRoot
和 SolidColorBrush
类型的 MyBrush
字段.如果您将 x:Name="LayoutRoot"
更改为 Name="LayoutRoot"
,那将不会有任何改变.Grid
有一个 Name
属性.但是尝试将 x:Name="MyBrush"
更改为 Name="MyBrush"
.这不起作用,因为 SolidColorBrush
没有 name 属性.使用上面的 Xaml,您可以执行如下代码:-
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;
}
打开InitializeComponent
的定义,看看自动生成的代码.
Open the definition of InitializeComponent
and take a look at the auto generated code.
这篇关于xaml 文件中控件的 x:name 和 name 有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!