如果可能的话,更好的做法是将您使用的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"
>
<!-- Note: no namespace prefix needed! -->
<YourCustomControl />
不管你和你的团队想要的。这只是一个本地别名。 – 2011-12-30 15:07:22
我是C#的新手,所以我问 - 人们通常选择什么? :-o – emesx 2011-12-30 15:12:50
就我个人而言,我不会创建层次结构:您在那里有'test','Converters'和'Validators'。但是,除了“任何作品”之外,没有其他的约定。 – 2011-12-30 15:19:30