Reputation: 12755
I have a C# WPF project, namespaced test
. How should I name sub-namespaces in 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"> ....
Here I have a convention of separating the sub-packages with a period.. Is it okay?
Kind regards,
e.
Upvotes: 3
Views: 2168
Reputation: 32715
A better practice, if possible, would be to separate the C# namespaces that you use from the WPF namespaces. This will also reduce the number of imports that you have. This can be done thanks to the XmlnsDefinition class.
<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">
In the AssemblyInfo.cs of your libraries, you'll just need to add:
[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")]
Note that this will only work if the classes are in a different assembly to the one you are referencing them from. Within the same assembly, you'll still need to use the C# namespaces.
You can even eliminate the imports altogether by adding your namespaces to the WPF XML namespace:
[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")]
That allows people to write:
<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 />
Upvotes: 10
Reputation: 38335
The typical WPF application really doesn't have a namespace convention for the XAML, other than the default xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
, the Blend design time namespace and xmlns:local
which will typically refer to the current namespace.
In the scenario you described above, I've seen/used a few variants, i.e.,
<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">
or
<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">
In the end, it's really up to whatever you and your team agree upon.
Upvotes: 2