Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF XAML合并默认命名空间定义_C#_Xml_Wpf_Xaml_Namespaces - Fatal编程技术网

C# WPF XAML合并默认命名空间定义

C# WPF XAML合并默认命名空间定义,c#,xml,wpf,xaml,namespaces,C#,Xml,Wpf,Xaml,Namespaces,在WPF应用程序中,您可以创建XmlnsDefinitions以将多个名称空间从另一个程序集映射到一个引用中 您可以利用它将自己的名称空间映射到Microsoft默认XmlnsDefinition,例如: [assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "MyLibrary.MyControls")] 这样,您甚至不需要在XAML文件中添加对名称空间的引用,因为它们已

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

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

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "MyLibrary.MyControls")]
这样,您甚至不需要在XAML文件中添加对名称空间的引用,因为它们已经映射到默认的“xmlns”

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

所以我可以把这个转过来:

<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("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System...")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System...")]
...
[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
名称空间:

  • 它没有映射到.NET名称空间,它是纯XML名称空间,是XAML语言的一部分

  • 这样做会导致冲突(
    x:Name
    Name


  • 说到冲突,像这样合并名称空间是自找麻烦的。您可能会遇到这些名称空间设计用来解决的名称冲突。

    Duplicate:。string innerxml=“xsi:noNamespaceSchemaLocation=\”amzn envelope.xsd\”xmlns:xsi=\”w3.org/2001/XMLSchema instance\”;使用(var writer=XmlWriter.Create(stream,settings)){writer.writeStarteElement(“AmazonDevelope”);writer.WriteRaw(innerxml);writer.WriteEndElement();}这个问题与我的问题无关。我将我的
    UserControl
    放入
    System.Windows.Controls
    。它对我不起作用:
    [assembly:System.Windows.Markup.XmlnsDefinition(“http://schemas.microsoft.com/winfx/2006/xaml/presentation“,“System.Windows.Controls”)]
    看起来设计器看到了我的类,但编译器没有看到。错误:
    XML命名空间中不存在标记“ContentViewer”http://schemas.microsoft.com/winfx/2006/xaml/presentation“
    dumb net库始终希望将默认名称空间作为最后一项。“您不能覆盖现有名称空间映射,因为它们已经存在于标准组件中”,我应该考虑到这一点。
    <Window x:Class="FoobarProject.MainWindow"
        xmlns="urn:foobar"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">