如果可能的话,更好的做法是将您使用的C#命名空间与WPF命名空间分开。这也将减少您所拥有的导入数量。这可以通过
XmlnsDefinition类来完成。
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="http://whatever.com/test">
在你的库的AssemblyInfo.cs中,你只需要添加以下内容:
[assembly: XmlnsDefinition("http://whatever.com/test", "test")]
[assembly: XmlnsDefinition("http://whatever.com/test", "test.Converters")]
[assembly: XmlnsDefinition("http://whatever.com/test", "test.Validators")]
[assembly: XmlnsDefinition("http://whatever.com/test", "test.CustomControls")]
请注意,只有在引用类的程序集与您所引用它们的程序集不同时,此方法才有效。在同一程序集中,仍需要使用C#命名空间。
您甚至可以通过将命名空间添加到WPF XML命名空间来完全消除导入:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "test")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "test.Converters")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "test.Validators")]
这使得人们可以编写:
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<YourCustomControl />
test
、Converters
和Validators
。但是再次强调,除了“任何有效的方法”之外,没有其他约定。 - Diego Mijelshon