2011-12-30 186 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