在WPF应用程序中,您可以创建XmlnsDefinitions将来自另一个程序集的多个命名空间映射到一个引用中。
您可以利用这一点将自己的命名空间映射到Microsoft默认的XmlnsDefinition,例如:
这样,您甚至不需要在XAML文件中添加对命名空间的引用,因为它们已经映射到默认的“xmlns”。因此,如果我有一个名为“MyControl”的控件,我可以像这样使用它(不需要任何命名空间或前缀):
我的问题是:我能将默认的Microsoft命名空间合并为一个吗?
例如:我想通过将“xmlns:x”命名空间合并到“xmlns”中来摆脱它。 我需要将“http://schemas.microsoft.com/winfx/2006/xaml”中的所有命名空间引用都改为“http://schemas.microsoft.com/winfx/2006/xaml/presentation”。
就像这样:
您可以利用这一点将自己的命名空间映射到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"
>
UserControl
放入了System.Windows.Controls
中。但是它对我不起作用:[assembly: System.Windows.Markup.XmlnsDefinition( "http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls" )]
看起来设计师能够看到我的类,但编译器却不能。错误:The tag 'ContentViewer' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'
。 - Denis535