在Wpf应用程序中区分x:Name和Name [英] Differentiate between x:Name and Name in Wpf application

查看:37
本文介绍了在Wpf应用程序中区分x:Name和Name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Wpf编程的初学者,我需要知道 Name x:Name

i'm beginner in Wpf programmation, i need to know the difference between Name and x:Name

<label Name="lab" ></label>

<label x:Name="lab" ></label>

以上两行之间有什么区别?在后面的代码中,如何区分 Name x:Name ?

what is the differences between the two lines above?. In code behind, How can i differentiate between Name and x:Name ?

推荐答案

请参阅它们指的是同一件事,但是在定义 UserControl 时必须使用x:前缀.

They refer to the same thing, but you have to use the x: prefix when defining a UserControl.

在WPF中,什么是x:Name和Name属性之间的区别?

是否有x:name和xaml文件中控件的名称有什么区别?

来自 Microsoft :

任何WPF应用程序场景都可以避免对x:Name的任何使用属性,因为Name依赖项属性在一些重要基类的默认XAML命名空间,例如因为FrameworkElement和FrameworkContentElement满足相同要求目的.仍然有一些常见的XAML和WPF场景,其中的代码在框架级别访问没有Name属性的元素是重要的.例如,某些动画和情节提要板支持类不支持Name属性,但通常需要在代码中引用以控制动画.你应该将x:Name指定为时间轴和如果您打算稍后从代码中引用它们,则在XAML中创建它们.如果名称可以作为类的属性使用,名称和x:Name可以是可以互换使用作为属性,但会导致解析异常如果两个都在同一个元素上指定.如果XAML是标记编译后,将在标记编译时发生异常,否则发生在负载上.可以使用XAML属性语法设置名称,并在使用SetValue的代码;但是请注意,将Name属性设置为代码不会在在大多数已加载XAML的情况下,XAML名称范围.与其尝试在代码中设置Name,不如使用NameScope方法.代码,针对适当的名称范围.名称也可以使用带有内部文本的属性元素语法,但这并不常见.在相反,不能使用XAML属性元素语法或x:Name设置使用SetValue的代码;只能使用对象,因为它是指令.

any WPF application scenarios can avoid any use of the x:Name attribute, because the Name dependency property as specified in the default XAML namespace for several of the important base classes such as FrameworkElement and FrameworkContentElement satisfies this same purpose. There are still some common XAML and WPF scenarios where code access to an element with no Name property at the framework level is important. For example, certain animation and storyboard support classes do not support a Name property, but they often need to be referenced in code in order to control the animation. You should specify x:Name as an attribute on timelines and transforms that are created in XAML, if you intend to reference them from code later. 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 can be set using XAML attribute syntax, and in code using SetValue; note however that setting the Name property in code does not create the representative field reference within the XAML namescope in most circumstances where the XAML is already loaded. Instead of attempting to set Name in code, use NameScope methods from code, against the appropriate namescope. 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.

这篇关于在Wpf应用程序中区分x:Name和Name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆