繁体   English   中英

WPF XAML合并默认名称空间定义

[英]WPF XAML merge default namespace definitions

在WPF应用程序中,您可以创建XmlnsDefinitions来将多个名称空间从另一个程序集中映射到一个引用中。

您可以利用它来将自己的名称空间映射到Microsoft默认的XmlnsDefinition,例如:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "MyLibrary.MyControls")]

这样,您甚至不需要在XAML文件中添加对名称空间的引用,因为它们已经映射到默认的“ xmlns”。

因此,如果我有一个名为“ MyControl”的控件,则可以这样使用它(没有任何名称空间或前缀):

<MyControl />

我的问题是 :我可以将默认的Microsoft命名空间合并为一个吗?

例如:我想通过将“ xmlns:x”命名空间合并到“ xmlns”中来摆脱它。 我必须将“ http://schemas.microsoft.com/winfx/2006/xaml ”中的所有命名空间引用为“ http://schemas.microsoft.com/winfx/2006/xaml/presentation ”。

像这样:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System...")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System...")]
...

所以我可以打开它:

<Window x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >

变成这个:

<Window Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    >

您无法覆盖现有名称空间映射,因为它们已经存在于标准组件中,但是您可以尝试将所有.NET名称空间合并到您自己的XML名称空间中。 像这样:

[assembly: XmlnsDefinition("urn:foobar", "System.Windows.Controls")]
[assembly: XmlnsDefinition("urn:foobar", "System.Windows.Documents")]
[assembly: XmlnsDefinition("urn:foobar", "System.Windows.Shapes")]
// ...
[assembly: XmlnsDefinition("urn:foobar", "FoobarLibrary.Controls")]
// ...

可能有效(我没有尝试过)。 您的XAML如下所示:

<Window x:Class="FoobarProject.MainWindow"
    xmlns="urn:foobar"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

请注意,您无法摆脱x名称空间:

  1. 它没有映射到.NET命名空间,它是纯XML命名空间,并且是XAML语言的一部分。

  2. 这样做会导致冲突( x:NameName )。

说到冲突,像这样合并名称空间带来了麻烦。 您可能会遇到名称冲突,这些名称空间旨在解决这些冲突。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM