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

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

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


当前回答

它们都是一样的东西,很多框架元素本身公开了name属性,但对于那些不公开的元素,你可以使用x:name -我通常只使用x:name,因为它适用于任何东西。

控件可以将名称本身公开为依赖属性(因为它们需要在内部使用该依赖属性),也可以选择不这样做。

更多细节在msdn这里和这里:

Some WPF framework-level applications might be able to avoid any use of the x:Name attribute, because the Name dependency property as specified within the WPF namespace for several of the important base classes such as FrameworkElement/FrameworkContentElement satisfies this same purpose. There are still some common XAML and framework scenarios where code access to an element with no Name property is necessary, most notably in certain animation and storyboard support classes. For instance, you should specify x:Name on timelines and transforms created in XAML, if you intend to reference them from code. If Name is available as a property on the class, Name and x:Name can be used interchangeably as attributes, but an error will result if both are specified on the same element.

其他回答

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.

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

它们都是一样的东西,很多框架元素本身公开了name属性,但对于那些不公开的元素,你可以使用x:name -我通常只使用x:name,因为它适用于任何东西。

控件可以将名称本身公开为依赖属性(因为它们需要在内部使用该依赖属性),也可以选择不这样做。

更多细节在msdn这里和这里:

Some WPF framework-level applications might be able to avoid any use of the x:Name attribute, because the Name dependency property as specified within the WPF namespace for several of the important base classes such as FrameworkElement/FrameworkContentElement satisfies this same purpose. There are still some common XAML and framework scenarios where code access to an element with no Name property is necessary, most notably in certain animation and storyboard support classes. For instance, you should specify x:Name on timelines and transforms created in XAML, if you intend to reference them from code. If Name is available as a property on the class, Name and x:Name can be used interchangeably as attributes, but an error will result if both are specified on the same element.

名称:

只能用于FrameworkElement和FrameworkContentElement的后代; 可以通过SetValue()和property-like在代码后面设置。

x:名称:

可以用于几乎所有XAML元素; 不能设置从 通过SetValue()隐藏代码;只能使用属性设置 对象的语法,因为它是一个指令。

在XAML中对一个FrameworkElement或FrameworkContentElement使用这两个指令将导致异常:如果XAML是标记编译,则异常将在标记编译时发生,否则将在加载时发生。

它们不是一回事。

x:Name是一个xaml概念,主要用于引用元素。当您为元素赋予x:Name xaml属性时,“指定的x:Name将成为处理xaml时在底层代码中创建的字段的名称,并且该字段包含对该对象的引用。”因此,它是一个由设计器生成的字段,默认情况下具有内部访问权限。

Name是FrameworkElement的现有字符串属性,以xaml属性的形式列出,与任何其他wpf元素属性一样。

因此,这也意味着x:Name可以用于更广泛的对象。这是一种使xaml中的任何东西都可以通过给定名称引用的技术。