有时,Name和x:Name属性似乎是可互换的。

那么,它们之间的决定性区别是什么?什么时候使用一种比另一种更可取?

以错误的方式使用它们会对性能或内存产生影响吗?


当前回答

The specified x:Name becomes the name of a field that is created in the underlying code when XAML is processed, and that field holds a reference to the object. In Silverlight, using the managed API, the process of creating this field is performed by the MSBuild target steps, which also are responsible for joining the partial classes for a XAML file and its code-behind. This behavior is not necessarily XAML-language specified; it is the particular implementation that Silverlight applies to use x:Name in its programming and application models.

阅读更多关于MSDN…

其他回答

我的研究是x:命名为全局变量。但是,Name为局部变量。这是否意味着x:Name可以在XAML文件的任何地方调用,但Name不能。 例子:

<StackPanel>
<TextBlock Text="{Binding Path=Content, ElementName=btn}" />
<Button Content="Example" Name="btn" />
</StackPanel>
<TextBlock Text="{Binding Path=Content, ElementName=btn}" />

你不能绑定属性内容的按钮名称是“btn”,因为它在StackPanel之外

x:Name和Name引用不同的命名空间。

x:name是对Xaml文件顶部默认定义的x命名空间的引用。

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

只是说Name使用下面默认的命名空间。

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

x:Name表示使用具有x别名的命名空间。X是默认值,大多数人会保留它,但您可以将其更改为任何您喜欢的值

xmlns:foo="http://schemas.microsoft.com/winfx/2006/xaml"

所以你的引用应该是foo:name

在WPF中定义和使用命名空间


好,我们换个角度看。假设您将一个按钮拖放到Xaml页面上。你可以用两种方式引用这个x:name和name。所有xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"和 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"是多个名称空间的引用。因为xaml拥有Control命名空间(不是100%),而presentation拥有FrameworkElement和Button类有一个继承模式:

Button : ButtonBase
ButtonBase : ContentControl, ICommandSource
ContentControl : Control, IAddChild
Control : FrameworkElement
FrameworkElement : UIElement, IFrameworkInputElement, 
                    IInputElement, ISupportInitialize, IHaveResources

因此,从FrameworkElement继承的任何东西都可以访问它的所有公共属性。在Button的例子中,它从FrameworkElement中获得它的Name属性,在层次结构树的最顶端。所以你可以说x:Name或Name,它们都将从FrameworkElement访问getter/setter。

MSDN参考

WPF定义了一个CLR属性,XAML处理器使用该属性将多个CLR名称空间映射到单个XML名称空间。XmlnsDefinitionAttribute属性被放置在生成程序集的源代码中的程序集级别。WPF程序集源代码使用此属性映射各种公共名称空间,例如System。Windows和System.Windows。控件添加到http://schemas.microsoft.com/winfx/2006/xaml/presentation名称空间。

所以程序集属性看起来像这样:

高清设备

[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")]  

这不是一个WPF项,而是一个标准的XML项,BtBh已经正确地回答了它,x指的是默认的名称空间。在XML中,如果不给元素/属性加上名称空间前缀,则假定您需要默认名称空间。 因此,只输入Name只不过是x:Name的缩写。关于XML名称空间的更多细节可以在链接文本中找到

我总是使用x:Name变量。 我不知道这是否会影响性能,我只是觉得它更容易,原因如下。 如果您有自己的用户控件驻留在另一个程序集中,只有“Name”属性并不总是足够的。这使得使用x:Name属性更加容易。

当您在XAML中声明Button元素时,您引用的是在windows运行时中定义的一个名为Button的类。

按钮有很多属性,如背景,文本,边距,.....和一个名为Name的属性。

现在,当您在XAML中声明一个Button时,就像创建一个匿名对象,该对象碰巧有一个名为Name的属性。

一般来说,你不能引用一个匿名对象,但是在WPF框架中,XAML处理器允许你通过给Name属性的任何值来引用该对象。

到目前为止一切顺利。

创建对象的另一种方法是创建命名对象而不是匿名对象。在这种情况下,XAML名称空间有一个名为Name的对象属性(由于它在XAML名称空间中,因此有X:),您可以设置该属性,以便识别对象并引用它。

结论:

Name是特定对象的属性,但X:Name是该对象的一个属性(有一个类定义了一个通用对象)。