WPF: Tired of xmlns="clr-namespace ..." ?!
I'm sure you all know the problem: When using custom objects or controls in XAML you need to add a xmlns:bla="clr-namespace:MyDotNetSpace" declaration to each XAML file (which can be thought of as the C# using statement).
Although this is now comfortabely supported by Visual Studio Intellisense, some other solution would be nicer to have..
As I always want to do things like "Microsoft" does ;-), I thought of ways how to add my .NET namespaces into such nice looking Uri namespaces..
I found the solution for it, which is really easy:
[assembly:XmlnsDefinition("https://blogs.msdn.com/knom/wpf/XmlnsDemo", "Knom.WPF.XmlNs")]
namespace Knom.WPF.XmlNs
{
...
}
Just decorate your namespace with this XmlnsDefiniton attribute and map an Xml-namespace to your .NET namespace.
Now you can simply reference the namespace by using:
xmlns:my="https://blogs.msdn.com/knom/wpf/XmlnsDemo"
Tired of using the prefix (like my:..)? For this there're two tricks:
[assembly:XmlnsPrefix("https://blogs.msdn.com/knom/wpf/XmlnsDemo", "max:")]
This will specify a default prefix of max: for the xml namespace. If you drag in the control in the designer, this prefix will be generated for the namespace.
An interesting, but dangerous option is multiple .NET <--> XML namespace mappings: You can map different .NET namespaces into one xml namespace.
Consequently you can map YOUR .NET namespace into the default xaml namespace like this:
[assembly: XmlnsDefinition("https://schemas.microsoft.com/winfx/2006/xaml/presentation", "Knom.WPF.XmlNs")]
This way your namespace is now part of the XML namespace normally included as default namespace. So you won't need to specify ANY prefix for your classes. Disadvantage: The transparency which class is included in which assembly is lost..
Comments
Anonymous
November 01, 2007
PingBack from http://msdnrss.thecoderblogs.com/2007/11/02/wpf-tired-of-xmlnsclr-namespace/Anonymous
April 27, 2008
The comment has been removedAnonymous
May 28, 2012
The comment has been removed