有时,Name和x:Name属性似乎是可互换的。
那么,它们之间的决定性区别是什么?什么时候使用一种比另一种更可取?
以错误的方式使用它们会对性能或内存产生影响吗?
有时,Name和x:Name属性似乎是可互换的。
那么,它们之间的决定性区别是什么?什么时候使用一种比另一种更可取?
以错误的方式使用它们会对性能或内存产生影响吗?
当前回答
X:如果有自定义控件,Name可能会导致内存问题。它将为NameScope条目保留一个内存位置。
我建议除非迫不得已,否则不要使用x:Name。
其他回答
名称:
只能用于FrameworkElement和FrameworkContentElement的后代; 可以通过SetValue()和property-like在代码后面设置。
x:名称:
可以用于几乎所有XAML元素; 不能设置从 通过SetValue()隐藏代码;只能使用属性设置 对象的语法,因为它是一个指令。
在XAML中对一个FrameworkElement或FrameworkContentElement使用这两个指令将导致异常:如果XAML是标记编译,则异常将在标记编译时发生,否则将在加载时发生。
X:如果有自定义控件,Name可能会导致内存问题。它将为NameScope条目保留一个内存位置。
我建议除非迫不得已,否则不要使用x:Name。
这不是一个WPF项,而是一个标准的XML项,BtBh已经正确地回答了它,x指的是默认的名称空间。在XML中,如果不给元素/属性加上名称空间前缀,则假定您需要默认名称空间。 因此,只输入Name只不过是x:Name的缩写。关于XML名称空间的更多细节可以在链接文本中找到
我的研究是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之外
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…