xml 每个 WPF 文件中的 xmlns 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/630468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is xmlns in every WPF file?
提问by Embedd_Khurja
What is xmlns?
什么是xmlns?
What role does it play in an XAML file when we create a WPF project?
当我们创建 WPF 项目时,它在 XAML 文件中扮演什么角色?
回答by JaredPar
xmlns is an XML, not necessarily XAML, construct which defines a namespace in which to resolve xml element names. Because it is defined without a qualifier, it is defining the default namespace by which an XML element name should be resolved.
xmlns 是一个 XML(不一定是 XAML)构造,它定义了一个命名空间,在其中解析 xml 元素名称。因为它是在没有限定符的情况下定义的,所以它定义了解析 XML 元素名称的默认名称空间。
In XAML you usually see the following entry. It defines the default namespace to be essentially WPF and all XML element names are hence resolved as WPF elements.
在 XAML 中,您通常会看到以下条目。它将默认命名空间定义为本质上是 WPF,因此所有 XML 元素名称都被解析为 WPF 元素。
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
It's also common to see non-default namespaces such as the following.
看到如下非默认命名空间也很常见。
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
This defines a qualified namespace for XAML specific elements. If you want an element or attribute name to be resolved within this namespace you should qualify it with x. For example
这定义了 XAML 特定元素的限定命名空间。如果您希望在此命名空间内解析元素或属性名称,则应使用 x 对其进行限定。例如
<StackPanel x:Name="foo" />
There are 2 name resolutions in this definition.
此定义中有 2 个名称解析。
- StackPanel - Because it's an unqualified name, it will be resolved in the default namespace which is WPF
- x:Name - Name is qualified with x and will be resolved within the XAML document.
- StackPanel - 因为它是一个非限定名称,它将在默认命名空间中解析,即 WPF
- x:Name - 名称用 x 限定,并将在 XAML 文档中解析。
回答by Joel Cochran
And you use xmlns to get a reference to your own namespaces within your XAML as well. One of the first things I do when creating a new WPF project is to add a reference to the project namespace:
并且您还使用 xmlns 在 XAML 中获取对您自己的命名空间的引用。创建新的 WPF 项目时,我做的第一件事是添加对项目命名空间的引用:
xmlns:local="clr-namespace:MyWpfProject"
Now I have access to any classes I may create within my project (like IValueConverters and DataTemplateSelectors) by using the "local:" prefix
现在,我可以使用“local:”前缀访问我可能在我的项目中创建的任何类(如 IValueConverters 和 DataTemplateSelectors)
<local:BooleanToColorConverter x:Key="booleanToColorConverter" DefaultBrush="Green" HighlightBrush="Red" />
Of course, you don't have to use "local", you can name it whatever you want. And you can add references to any other namespace you need the same way.
当然,您不必使用“local”,您可以随意命名。并且您可以以相同的方式添加对您需要的任何其他命名空间的引用。
回答by Anthony Brien
You can also map multiple CLR namespaces together into one XML namespace by adding XmlnsDefinitionAttributeto your assemblies. This is what the WPF team did, by mapping a lot of namespaces under System.Windows like this:
您还可以通过将多个 CLR 命名空间添加XmlnsDefinitionAttribute到程序集来将多个 CLR 命名空间映射到一个 XML 命名空间中。这就是 WPF 团队所做的,通过在 System.Windows 下映射大量命名空间,如下所示:
[XmlnsDefinitionAttribute(
"http://schemas.microsoft.com/winfx/2006/xaml/presentation",
"System.Windows.Controls")]
[XmlnsDefinitionAttribute(
"http://schemas.microsoft.com/winfx/2006/xaml/presentation",
"System.Windows.Ink")]
This syntax can simplify your XAML, but be careful not to have classes with the same name in the CLR namespaces you merge together.
此语法可以简化您的 XAML,但请注意不要在合并在一起的 CLR 命名空间中包含同名的类。
回答by Bryan Bailliache
As taken from MSDNThe root element also contains the attributes xmlns and xmlns:x. These attributes indicate to a XAML processor which XML namespaces contain the element definitions for elements that the markup will reference. The xmlns attribute specifically indicates the default XML namespace.
取自MSDN根元素还包含属性 xmlns 和 xmlns:x。这些属性向 XAML 处理器指示哪些 XML 命名空间包含标记将引用的元素的元素定义。xmlns 属性专门指示默认的 XML 命名空间。
This usage of xmlns to define a scope for usage and mapping is consistent with the XML 1.0 specification.
这种使用 xmlns 来定义使用范围和映射的用法与 XML 1.0 规范一致。
Hope this helps.
希望这可以帮助。

