有时,Name和x:Name属性似乎是可互换的。
那么,它们之间的决定性区别是什么?什么时候使用一种比另一种更可取?
以错误的方式使用它们会对性能或内存产生影响吗?
有时,Name和x:Name属性似乎是可互换的。
那么,它们之间的决定性区别是什么?什么时候使用一种比另一种更可取?
以错误的方式使用它们会对性能或内存产生影响吗?
当前回答
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")]
其他回答
其中一个答案是x:name将在不同的程序语言中使用,如c#,而name将用于框架。说实话,我听起来就是这样。
Name can also be set using property element syntax with inner text, but that is uncommon. In contrast, x:Name cannot be set in XAML property element syntax, or in code using SetValue; it can only be set using attribute syntax on objects because it is a directive. If Name is available as a property on the class, Name and x:Name can be used interchangeably as attributes, but a parse exception will result if both are specified on the same element. If the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load.
唯一的区别是,如果你在同一个程序集中使用用户控件到一个控件中,那么Name将不能识别你的控件,你将得到一个错误“在同一个程序集中使用x:Name控件”。 所以x:Name是WPF中命名控件的WPF版本。Name只是用作Winform Legacy。他们想在WPF和winforms中区分控件的命名,因为他们使用Xaml中的属性来标识控件与其他程序集,他们使用x:表示控件的名称。
请记住,不要为控件命名,因为它在内存中是空白的,它会给你一个警告,说明名称已应用于控件,但它从未使用过。
在XAML中实际上只有一个名称,即x: name。一个框架,比如WPF,可以通过在类上使用RuntimeNamePropertyAttribute将它的一个属性映射到XAML的x:Name属性,这个类指定一个类属性映射到XAML的x:Name属性。
这样做的原因是允许在运行时已经有“Name”概念的框架,例如WPF。例如,在WPF中,FrameworkElement引入了一个Name属性。
一般来说,类不需要存储x: name的名称以使其可用。所有x:Name的意思是XAML是生成一个字段来存储类后面的代码中的值。运行时对映射的处理依赖于框架。
那么,为什么有两种方法来做同样的事情呢?简单的答案是,因为两个概念映射到一个属性上。WPF希望在运行时保留元素的名称(可以通过Bind等方式使用),XAML需要知道您希望类后面代码中的字段可以访问哪些元素。WPF通过将Name属性标记为x:Name的别名将这两者联系在一起。
在未来,XAML会有更多x:Name的用途,比如允许你通过名称引用其他对象来设置属性,但在3.5和更早的版本中,它只用于创建字段。
你应该使用其中一种还是另一种实际上是一个风格问题,而不是技术问题。我将把这个留给其他人来推荐。
请参见AutomationProperties。Name VS x:Name, AutomationProperties。可访问性工具和一些测试工具使用Name。
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")]