What happens when I import a namespace in XAML?

I have a software project I am working on in WPF with several other "Helper" assemblies, structured as follows:

  • Controls (WPF Custom Control Library)
  • Library (C# Class Library)
  • Main Project (WPF Application)

Within the "Controls" assembly, I have several namespaces grouping related types of controls together. This makes it easy to manage which code is where in this assembly, since the namespaces are the same as the folder names.

However, when I am using these controls in other parts of the project, this can get a little confusing, particularly as I sometimes have to use multiple namespace declarations in the same file to reference different parts of the "Controls" assembly.

I have been wondering whether it would make more sense to put all of my controls into a more centralised namespace that I can use everywhere. The barrier against doing this is that every time I use that namespace it would reference all of the controls in the assembly. However, it would make my XAML a lot cleaner.

So, what actually happens when I import a namespace reference in XAML? Does it work the same way as in C#? What happens if I have a bunch of unused references sitting around? And would it be okay from a performance perspective if I put all of my controls into the same namespace?

Alternatively, am I just being silly and asking completely the wrong question?

1 answer

  • answered 2017-12-06 14:55 Steven Rands

    I have been wondering whether it would make more sense to put all of my controls into a more centralised namespace that I can use everywhere.

    There's really no need to do that.

    The barrier against doing this is that every time I use that namespace it would reference all of the controls in the assembly. [...] So, what actually happens when I import a namespace reference in XAML? [...] What happens if I have a bunch of unused references sitting around?

    Nothing significant. XAML namespaces are just the mechanism by which object references get resolved at compile-time. Any unused references are ultimately ignored by the compiler. It has no impact upon performance.

    And would it be okay from a performance perspective if I put all of my controls into the same namespace?

    Performance wouldn't be affected either way, but I would suggest keeping your existing file & folder structure.

    What you could do is make use of an XmlnsDefinitionAttribute to put controls from different C# namespaces into the same logical XAML namespace. This attribute is placed in the AssemblyInfo.cs file, like so:

    [assembly: XmlnsDefinition("http://mycompany.com/controls", "MyCompany.Controls.Core")]
    [assembly: XmlnsDefinition("http://mycompany.com/controls", "MyCompany.Controls.Primitives")]
    

    In an assembly that references this assembly, you can then do something like the following:

    ...
    xmlns:mycontrols="http://mycompany.com/controls"
    ...
    <mycontrols:Foo ... />
    <mycontrols:Bar ... />
    

    Hence the Foo control could live in the C# namespace MyCompany.Controls.Core and the Bar control could live in the C# namespace MyCompany.Controls.Primitives, but both could be referenced using the same XAML namespace (http://mycompany.com/controls, here aliased with the mycontrols prefix).