WPF - globally add xaml namespace declaration
Asked Answered
D

2

15

I have quite a huge WPF application with a lot of XAML files. Every single XAML file has from 5 to 10 clr to xml namespace mappings xmlns:abc="clr-namespace:Abcdef".
It looks awful and is a pain to write in each and every file.

Is there a way to define those globally?

Despatch answered 27/10, 2011 at 6:55 Comment(0)
R
14

There is no way to define them globally across files. This is a limitation of XML; XAML is a subset of it.

However you can clean them up a bit using XmlnsDefinition

See this article: http://zachbonham.blogspot.com/2010/04/organize-xaml-namespace-declarations.html

If you started with this XAML:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:treeView="clr-namespace:MaryKay.SamPortal.Common.UI.TreeView.Views;assembly=MaryKay.SamPortal.Common.UI"
    xmlns:infoBar="clr-namespace:MaryKay.SamPortal.Common.UI.InfoBar.Views;assembly=MaryKay.SamPortal.Common.UI">
  <infoBar:InformationBar DataContext="{Binding InfoBar}"/>
</UserControl>

And added these XmlnsDefinition attributes:

[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.InfoBar.Views")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.RoleGroupPicker.Views")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.BetterPopup")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.TextEditor")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.Converters")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.Documents")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.SplashScreen")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.TemplateSelector")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.ModalDialog")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.ConsultantSearch.Views")]
// etc...

You could end up with this XAML instead:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:commonUI="urn:marykay-samportal-common-ui">
  <commonUI:InformationBar DataContext="{Binding InfoBar}"/>
</UserControl>
Rhines answered 27/10, 2011 at 7:42 Comment(2)
I'm not having any luck getting this to work when referencing a namespace defined in a different assembly.Antilogy
@Antilogy #4225572Rhines
C
0

Suppose you have two "WPF class library projects"

  1. ButtonLibrary
  2. TextBoxLibrary

Now you want to reference the classes or controls defined in those assemblies to your main WPF application using a single xmlns:myCtrl prefix.

For this you have to add an AssemblyInfo.cs file in both the "WPF class library project" and then you have to map all the CLR namespaces in those projects to one or more XML namespaces.

VisualStudio Solution beforeenter image description here

VisualStudio Solution after using XmlnsDefinition enter image description here

Cornejo answered 23/2 at 13:10 Comment(1)
OMG a new answer 13 years later..Despatch

© 2022 - 2024 — McMap. All rights reserved.