2011-12-30 194 views
2

我有一个C#WPF项目,命名空间为test。我应该如何命名XAML中的子命名空间?XAML命名空间命名约定

<Window x:Class="test.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:local="clr-namespace:test" 
    xmlns:local.c="clr-namespace:test.Converters" 
    xmlns:local.v="clr-namespace:test.Validators"  
    Title="MainWindow" Height="360" Width="640"> .... 

在这里,我有一个约定,用一段时间分隔子包。可以吗?

亲切的问候,

e。

+0

不管你和你的团队想要的。这只是一个本地别名。 – 2011-12-30 15:07:22

+0

我是C#的新手,所以我问 - 人们通常选择什么? :-o – emesx 2011-12-30 15:12:50

+0

就我个人而言,我不会创建层次结构:您在那里有'test','Converters'和'Validators'。但是,除了“任何作品”之外,没有其他的约定。 – 2011-12-30 15:19:30

回答

1

典型的WPF应用程序确实没有XAML的名称空间约定,除了默认xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",Blend设计时间名称空间和xmlns:local,它们通常会引用当前名称空间。

在你上面描述的场景,我见过/使用的几个变种,即

<Window x:Class="test.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:local="clr-namespace:test" 
    xmlns:c="clr-namespace:test.Converters" 
    xmlns:v="clr-namespace: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"  
    xmlns:local="clr-namespace:test" 
    xmlns:conv="clr-namespace:test.Converters" 
    xmlns:val="clr-namespace:test.Validators"> 

最后,这真的取决于不管你和你的团队达成一致。

+1

谢谢你的简洁回答:) – emesx 2011-12-30 16:33:46

9

如果可能的话,更好的做法是将您使用的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 /> 
+0

谢谢,我所有的课程都在同一个程序集 – emesx 2011-12-30 19:55:02