首页 > 解决方案 > WPF - Defining a xmlns based on another xmlns

问题描述

Is there any way to declare a namespace like this :

xmlns:views="clr-namespace:xxx.xxx.Views"
xmlns:someNestedViews="views.SomeNestedView"

The objective is to speed up nested/repeated xmlns declarations and make them easier to read/maintain.

标签: xmlwpfnamespaceshierarchy

解决方案


不,您需要包含整个 CLR 命名空间:xmlns:someNestedViews="xxx.xxx.Views.SomeNestedView". 该clr-namespace:语法将 XAML 命名空间映射到 CLR 命名空间。它不映射到另一个 XML 命名空间。

如果您是命名空间中的类的作者,则Views可以使用XmlnsDefinitionAttribute指定 XAML 命名空间和几个不同的 CLR 命名空间之间的映射,例如:

[assembly: XmlnsDefinition("http://yourlibrary.com/", "xxx.xxx.Views.")]
[assembly: XmlnsDefinition("http://yourlibrary.com/", "xxx.xxx.Views.SomeNestedView")]

这减轻了 XAML 中的负担:

xmlns:viewsAndNestedViews="http://yourlibrary.com/"

推荐阅读