简体   繁体   English

将默认的xml名称空间(xmlns)更改为自定义名称空间

[英]Change default xml namespace(xmlns) to a custom one

In a XAML file we usually use the default setting xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 在XAML文件中,我们通常使用默认设置xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

How can I define a custom xmlns instead of the default one. 如何定义自定义xmlns而不是默认的xmlns。 Such as xmlns="http://www.mypage.com" 例如xmlns="http://www.mypage.com"

I don't mean add to a new xmlns, but replace the default one. 我的意思不是添加到新的xmlns,而是替换默认的。

It's not really clear what your problem is... if you want to use the default namespace somehow different than suggested, just prefix the (normally) default namespace with a name: 尚不清楚您的问题是什么...如果您要使用默认名称空间与建议的方式有所不同,只需在(通常)默认名称空间前面加上一个名称即可:

<def:Window x:Class="WpfTests_2.MainWindow"
        xmlns:def="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns="clr-namespace:WpfTests_2"
        Title="MainWindow">
    <def:Window.Resources>
        <CustomBox x:Key="myLocalCustomBox"/>
    </def:Window.Resources>

    <def:Grid x:Name="grid1">
    </def:Grid>
</def:Window>

In this example, I make the project local namespace WpfTests_2 the default and name the (normal) default namespace as def . 在此示例中,我将项目本地名称空间WpfTests_2为默认名称,并将(常规)默认名称空间命名为def

For a typical WPF application, this is a bad idea because you will use many controls and you need to prefix your namespace name every time . 对于典型的WPF应用程序来说, 这是一个坏主意,因为您将使用许多控件,并且每次都需要为名称空间名称添加前缀 But it is certainly possible. 但这当然是可能的。

If you don't use anything from the http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace (no Window , no UserControl , no [whatever control]), you can remove/replace it without redefining it with a prefix. 如果您不使用http://schemas.microsoft.com/winfx/2006/xaml/presentation命名空间中的任何内容(没有Window ,没有UserControl ,没有[任何控件]),则可以删除/替换它而无需重新定义它带前缀。

I think it is possible, by second part doc below: 我认为有可能,通过下面的第二部分文档:

For almost all of the examples given in the WPF sections of the SDK, the default XAML namespace is mapped to the WPF namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation.

Since almost all , That is there exists few example are not using this url, so it's possible. 由于almost all ,也就是说, almost all没有示例不使用此url,因此是可能的。

Docs Of Microsoft 微软文件

The WPF and XAML Namespace Declarations WPF和XAML命名空间声明

Within the namespace declarations in the root tag of many XAML files, you will see that there are typically two XML namespace declarations. 在许多XAML文件的根标记中的名称空间声明中,您将看到通常有两个XML名称空间声明。 The first declaration maps the overall WPF client / framework XAML namespace as the default: 第一个声明将整个WPF客户端/框架XAML命名空间映射为默认名称:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns =“ http://schemas.microsoft.com/winfx/2006/xaml/presentation”

The second declaration maps a separate XAML namespace, mapping it (typically) to the x: prefix. 第二个声明映射一个单独的XAML命名空间,(通常)将其映射到x:前缀。

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x =“ http://schemas.microsoft.com/winfx/2006/xaml”

Mapping to Custom Classes and Assemblies 映射到自定义类和程序集
... ...

Another Doc : 另一个文件

The root element also contains the attributes xmlns and xmlns:x . 根元素还包含属性xmlnsxmlns:x These attributes indicate to a XAML processor which XAML namespaces contain the type definitions for backing types that the markup will reference as elements. 这些属性向XAML处理器指示哪些XAML命名空间包含标记将引用为元素的支持类型的类型定义。

The xmlns attribute specifically indicates the default XAML namespace. xmlns属性专门指示默认的XAML命名空间。 Within the default XAML namespace, object elements in the markup can be specified without a prefix. 在默认的XAML命名空间内,可以在标记中指定对象元素而没有前缀。 For most WPF application scenarios, and for almost all of the examples given in the WPF sections of the SDK, the default XAML namespace is mapped to the WPF namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation . 对于大多数WPF应用程序场景,以及SDK的WPF部分中给出的几乎所有示例,默认的XAML名称空间都映射到WPF名称空间 http://schemas.microsoft.com/winfx/2006/xaml/presentation

The xmlns:x attribute indicates an additional XAML namespace, which maps the XAML language namespace http://schemas.microsoft.com/winfx/2006/xaml . xmlns:x属性指示附加的XAML名称空间,该名称空间映射XAML语言名称空间http://schemas.microsoft.com/winfx/2006/xaml

This usage of xmlns to define a scope for usage and mapping of a namescope is consistent with the XML 1.0 specification. xmlns用来定义名称范围的使用和映射范围的这种用法与XML 1.0规范一致。 XAML namescopes are different from XML namescopes only in that a XAML namescope also implies something about how the namescope's elements are backed by types when it comes to type resolution and parsing the XAML. XAML名称范围与XML名称范围的不同之处仅在于XAML名称范围还暗示了有关在类型解析和解析XAML时如何使用类型支持名称范围的元素。

Note that the xmlns attributes are only strictly necessary on the root element of each XAML file. 请注意,只有在每个XAML文件的根元素上才必须严格使用xmlns属性。 xmlns definitions will apply to all descendant elements of the root element (this behavior is again consistent with the XML 1.0 specification for xmlns .) xmlns attributes are also permitted on other elements underneath the root, and would apply to any descendant elements of the defining element. xmlns定义将应用于根元素的所有后代元素(此行为再次与xmlns的XML 1.0规范一致。) xmlns属性也允许在根下的其他元素上使用,并且将应用于定义元素的任何后代元素。 However, frequent definition or redefinition of XAML namespaces can result in a XAML markup style that is difficult to read. 但是,频繁定义或重新定义XAML名称空间可能会导致XAML标记样式难以阅读。

The WPF implementation of its XAML processor includes an infrastructure that has awareness of the WPF core assemblies. 其XAML处理器的WPF实现包括一个了解WPF核心程序集的基础结构。 The WPF core assemblies are known to contain the types that support the WPF mappings to the default XAML namespace. 已知WPF核心程序集包含支持WPF到默认XAML命名空间的映射的类型。 This is enabled through configuration that is part of your project build file and the WPF build and project systems. 通过配置可以启用此功能,该配置是项目构建文件以及WPF构建和项目系统的一部分。 Therefore, declaring the default XAML namespace as the default xmlns is all that is necessary in order to reference XAML elements that come from WPF assemblies. 因此,为了引用来自WPF程序集的XAML元素,必须将默认XAML命名空间声明为默认xmlns

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

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