WPF 中的 x: 是做什么用的?
我见过很多次了,但我不知道这意味着什么。我想说这与命名空间有关?我已经用谷歌搜索过它,但我不清楚它的目的是什么。
谁能解释一下它是什么以及通常在什么情况下使用?
谢谢
编辑:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
但是,例如,在上面的代码中,我将 x
定义为第三行 XAML 命名空间的别名,尽管我使用相同的 x
就在代码的第一行。这是怎么发生的?它不关心事物出现的顺序?
编辑2: 如果我错了请纠正我:
窗口 x:Class="WpfApplication8.MainWindow"
上面的代码会将一个从 Window 派生的类放入 x:WpfApplication8.MainWindow 命名空间中,而
窗口 x:Name="abc"
将在 x 命名空间中放入名为 abc 的 Window 类的实例。是这样吗?
如果我是对的,如果我没有使用 x
别名,那么类(第一种情况)和实例(第二种情况)会被放入哪里?无处可去,有点像匿名类型?它们被使用了,但是它们所在的位置没有被定义?
I've seen it a lot and I don't know what it means. I'd say it has something to do with namespaces? I've already googled it but it wasn't clear to me what it's purpose is.
Could anyone explain what it is and in which situations it is usually used?
Thanks
edit:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
But for example, in the above code, I am defining x
as an alias for the XAML namespace on the third line, although I am using that same x
right in the first line of code. How does this happen? It doesn't care for the order in which things appear?
edit2:
Correct me if I'm wrong:
Window x:Class="WpfApplication8.MainWindow"
The above code will put a class derived from Window in the x:WpfApplication8.MainWindow namespace, while
Window x:Name="abc"
will put in the x namespace an instance of the Window class named abc. Is that right?
If I'm right, had I not used the x
alias, where would have both the class(first case) and instance(second) case have been put in? Nowhere, a bit like anonimous types? They are used but the place where they are is not defined?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是对的,它是 XML 命名空间别名。如果您查看 xaml 文件的顶部,您会发现它映射到
http://schemas.microsoft.com/winfx/2006/xaml
命名空间。稍后使用前缀允许 xaml 解析器定位在该命名空间中定义的类,而无需您键入整个内容。这与 C# 中的using alias =very.long.namespace;
语法没有什么不同。通常需要为要在 xaml 文件中使用的每个命名空间设置不同的别名。使用 PRISM 时常见的一种是将
cal
映射到clr-namespace: Microsoft.Practices.Composite.Presentation.Regions;程序集=Microsoft.Practices.Composite.Presentation
;然后,您可以使用cal:RegionManager
访问在该 CLR 命名空间中定义的RegionManager
类。使用 Expression Blend 时,您经常会发现它添加了xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
和xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/2006”
以允许某些设计时功能(为画板上的用户控件设置宽度和高度)。对问题编辑的回应:您可能想看看 XML 命名空间教程,这是 xaml 继承此功能的地方。在您的示例中,您需要停止将这些行视为单独的指令,就像它们在程序代码中一样,而是将元素视为一个整体。我想说的是,因为
xmlns:x
出现在Window
元素中,所以该元素及其包含的每个子元素(它的实际行)都可以访问它出现并不重要,因为 xaml 不像过程代码那样从上到下执行。对问题编辑 2 的回复: x:Class 属性< /a> 告诉编译器从 xaml 文件生成的分部类的名称应该是什么(如果这就是您所说的“将放置一个从 Window 派生的类...”的意思,那么您是对的)。
您还没有完成第二部分,即 x:Name 属性告诉编译器在类中生成一个字段,其中包含对该属性所附加的元素的引用。这意味着,在您的示例中,您的代码隐藏文件将能够使用
this.abc
来引用 xaml 标记中定义的 Window 元素。You're correct, it's an XML namespace alias. If you take a look at the top of your xaml file, you'll find that it maps to the
http://schemas.microsoft.com/winfx/2006/xaml
namespace. Using the prefix later on allows the xaml parser to locate classes defined in that namespace without you having to type out the entire thing. It's not unlike theusing alias = very.long.namespace;
syntax in C#.You normally need to set up a different alias for each namespace you intend to use in the xaml file. A common one when using PRISM is mapping
cal
toclr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation
; you can then access theRegionManager
class defined in that CLR namespace by usingcal:RegionManager
. When using Expression Blend, you'll often find that it addsxmlns:d="http://schemas.microsoft.com/expression/blend/2008"
andxmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
to allow for some of it's design time functionality (setting a width and height for the user control on the artboard).Response to question edit: You might want to take a look at an XML Namespace tutorial, which is where xaml inherits this functionality from. In your example, you need to stop thinking of the lines as being individual instructions as they would be in procedural code and rather consider the element as a whole. What I'm trying to say is that because
xmlns:x
appears in theWindow
element, it is accessible to that element and every child element it contains, the actual line it appears on is not important as xaml is not executed from top to bottom like procedural code is.Response to question edit 2: The x:Class attribute tells the compiler what the name of the partial class generated from the xaml file should be (if that is what you meant by "will put a class derived from Window...", then you're correct).
You're way off on the second part, the x:Name attribute tells the compiler to generate a field in the class containing a reference to the element to which the attribute is attached. What this means is that, in your example, your code behind file will be able to use
this.abc
to refer to the Window element defined in the xaml markup.它是命名空间定义别名。
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
它的作用很像速记或
using
/import声明。
通过在标签属性中定义命名空间,ala
x:Name
,您可以保证在正确的空间中使用正确的属性。您可能已经注意到,当您希望使用自定义 WPF 控件时,必须添加自己的程序集命名空间别名。
It's a namespace definition alias.
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
It acts much like a short-hand or
using
/import
statement.by defining the namespace in a tags attributes, ala
x:Name
, you are guaranteeing that you are using the correct attribute in the correct space.You may have noticed that you have to add your own assembly namespace alias when you wish to use a custom WPF control.