4

In a WPF application, you can create XmlnsDefinitions to map multiple namespaces from another assembly into one reference .

You can take advantage of that to map your own namespaces to the Microsoft default XmlnsDefinition, for example:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "MyLibrary.MyControls")]

This way, you don't even need to add a reference to your namespace in the XAML file, because they are already mapped to the default "xmlns".

So, if I had a control called "MyControl", I can use it like this (without any namespaces or prefixes):

<MyControl />

My question is: Can I merge the default Microsoft namespaces into a single one?

For example: I want to get rid of the "xmlns:x" namespace by merging it into "xmlns". I would have to reference all the namespaces in "http://schemas.microsoft.com/winfx/2006/xaml" to "http://schemas.microsoft.com/winfx/2006/xaml/presentation".

Like this:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System...")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System...")]
...

So I can turn this:

<Window x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >

Into this:

<Window Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    >
3
  • Duplicate : stackoverflow.com/questions/47427705/…. string innerxml = "xsi:noNamespaceSchemaLocation=\"amzn-envelope.xsd\" xmlns:xsi=\"w3.org/2001/XMLSchema-instance\""; using (var writer = XmlWriter.Create(stream, settings)) { writer.WriteStartElement("AmazonEnvelope"); writer.WriteRaw(innerxml); writer.WriteEndElement(); } Commented Nov 23, 2017 at 0:52
  • That question has nothing to do with mine. Commented Dec 21, 2017 at 17:45
  • I put my UserControl into System.Windows.Controls. And it does not work for me: [assembly: System.Windows.Markup.XmlnsDefinition( "http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls" )] Looks like designer is seeing my class, but compiler not. Error: The tag 'ContentViewer' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation' Commented Mar 16, 2020 at 23:51

1 Answer 1

3

You can't override existing namespace mappings as they are already present in the standard assembies, but you can try merging all .NET namespaces into your own XML namespace. Like this:

[assembly: XmlnsDefinition("urn:foobar", "System.Windows.Controls")]
[assembly: XmlnsDefinition("urn:foobar", "System.Windows.Documents")]
[assembly: XmlnsDefinition("urn:foobar", "System.Windows.Shapes")]
// ...
[assembly: XmlnsDefinition("urn:foobar", "FoobarLibrary.Controls")]
// ...

It may work (I haven't tried). Your XAML will look like this:

<Window x:Class="FoobarProject.MainWindow"
    xmlns="urn:foobar"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

Note that you can't get rid of x namespace:

  1. It isn't mapped to .NET namespace, it's pure XML namespace and part of XAML language.

  2. Doing so would cause conflicts (x:Name vs. Name).

Speaking of conflicts, merging namespaces like this is asking for trouble. You are likely to run into name conflicts which these namespaces are designed to solve.

Sign up to request clarification or add additional context in comments.

2 Comments

The dumb net library always wants the default namespace as the last item.
"You can't override existing namespace mappings as they are already present in the standard assembies", I should have thought about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.