简体   繁体   English

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

[英]WPF XAML merge default namespace definitions

In a WPF application, you can create XmlnsDefinitions to map multiple namespaces from another assembly into one reference . 在WPF应用程序中,您可以创建XmlnsDefinitions来将多个名称空间从另一个程序集中映射到一个引用中。

You can take advantage of that to map your own namespaces to the Microsoft default XmlnsDefinition, for example: 您可以利用它来将自己的名称空间映射到Microsoft默认的XmlnsDefinition,例如:

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

This way, you don't even need to add a reference to your namespace in the XAML file, because they are already mapped to the default "xmlns". 这样,您甚至不需要在XAML文件中添加对名称空间的引用,因为它们已经映射到默认的“ xmlns”。

So, if I had a control called "MyControl", I can use it like this (without any namespaces or prefixes): 因此,如果我有一个名为“ MyControl”的控件,则可以这样使用它(没有任何名称空间或前缀):

<MyControl />

My question is : Can I merge the default Microsoft namespaces into a single one? 我的问题是 :我可以将默认的Microsoft命名空间合并为一个吗?

For example: I want to get rid of the "xmlns:x" namespace by merging it into "xmlns". 例如:我想通过将“ xmlns:x”命名空间合并到“ xmlns”中来摆脱它。 I would have to reference all the namespaces in " http://schemas.microsoft.com/winfx/2006/xaml " to " http://schemas.microsoft.com/winfx/2006/xaml/presentation ". 我必须将“ http://schemas.microsoft.com/winfx/2006/xaml ”中的所有命名空间引用为“ http://schemas.microsoft.com/winfx/2006/xaml/presentation ”。

Like this: 像这样:

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

So I can turn this: 所以我可以打开它:

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

Into this: 变成这个:

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

You can't override existing namespace mappings as they are already present in the standard assembies, but you can try merging all .NET namespaces into your own XML namespace. 您无法覆盖现有名称空间映射,因为它们已经存在于标准组件中,但是您可以尝试将所有.NET名称空间合并到您自己的XML名称空间中。 Like this: 像这样:

[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")]
// ...

It may work (I haven't tried). 可能有效(我没有尝试过)。 Your XAML will look like this: 您的XAML如下所示:

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

Note that you can't get rid of x namespace: 请注意,您无法摆脱x名称空间:

  1. It isn't mapped to .NET namespace, it's pure XML namespace and part of XAML language. 它没有映射到.NET命名空间,它是纯XML命名空间,并且是XAML语言的一部分。

  2. Doing so would cause conflicts ( x:Name vs. Name ). 这样做会导致冲突( x:NameName )。

Speaking of conflicts, merging namespaces like this is asking for trouble. 说到冲突,像这样合并名称空间带来了麻烦。 You are likely to run into name conflicts which these namespaces are designed to solve. 您可能会遇到名称冲突,这些名称空间旨在解决这些冲突。

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

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