XAML命名空间命名约定

eme*_*esx 2 xml wpf xaml naming-conventions

我有一个名为namespace的C#WPF项目test.我应该如何在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"> ....
Run Code Online (Sandbox Code Playgroud)

在这里,我有一个用子句分隔子包的约定..可以吗?

亲切的问候,

Pau*_*ell 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">
Run Code Online (Sandbox Code Playgroud)

在库的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")]
Run Code Online (Sandbox Code Playgroud)

请注意,这仅适用于类与您引用它们的程序集不同的程序集.在同一个程序集中,您仍然需要使用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")]
Run Code Online (Sandbox Code Playgroud)

这允许人们写:

<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 />
Run Code Online (Sandbox Code Playgroud)