This tutorial shows how to use the XmlNamespace attribute to create predefined XAML namespace based on a custom URI.
- [Narrator] There is a feature in xaml namespaces that lets a developer predefine a URI that maps a .net or WinRT namespace to a xaml namespace. We've seen this. That's how this default namespace in xaml works. Microsoft mapped this URI to a bunch of .net namespaces spread across multiple .net assemblies. Because they did that, now I can easily use the grid the textbox and the ellipse and other elements in my xaml file. The system for doing this mapping is available for any developer.
This is useful for some teams. For example, imagine you work for a control vendor like Infragistics, DevExpress, or Telerik. It would be convenient for you to use this mapping feature for your control libraries. That would make it easy to access the controls in a xaml file. That's what I'll show you in this video, how to do that mapping. Let's review what I have in this solution. I have three projects. This is my main project, the Wpf application, and I'll be using this Window1.xaml file. Then I have two control libraries, in separate assemblies.
I have this one called CoursewareControls and I have one called CoursewareGraphics. So, I am to put two separate complied assemblies that contain controls. Inside this first project, I have a gauge that looks like this, and I have an IP address control. In the CoursewareGraphics, I have more graphic-oriented controls. I have something called a WaterBackground. It looks like this, and I have an Octopod which is a whimsical creature that I created. To do the mapping go to your Properties section of a project and open this file, AssemblyInfo.cs.
If you don't see it, make sure you expand this node here. Double click on this file. This is where you set your properties for your assembly. For instance, it has the AssemblyTitle, your AssemblyCopyright, and other settings in here. And on line 19 you can see where I do the mapping. It's done through an attribute called XmlnsDefinition. This takes two parameters. The first parameter is the URI that I'm using. This is a unique URI. Typically to make a unique we use a domain that our company uses.
So, I'm using this fictitious domain here, Courseware.com and then this string on the end, CoursewareLib. The second part of this attribute is the namespace, the .net namespace that contains the controls that I want to use. So, let's go and review the gauge control. Gauge is in a namespace called Courseware.Controls. IpAddress is in the same namespace, Courseware.Controls. Well, that's what I'm setting down here, is I'm saying inside this assembly I want you to expose all of the controls or classes that are inside CoursewareControls to be able to use them through this URI.
Now another team that works our same company, that's working on this CoursewareGraphics, they've done this same thing. So, if you look at Octopod it's in Courseware.Graphics. WaterBackgroud is in Courseware.Graphics. We'll go over here to that assembly info file, here inside the Properties section, there's a second project. Use another Xmlns definition attribute here using exactly the same URI. Here, I'm saying use the controls that are inside Courseware.Graphics. Save my project, and then I'll compile it.
Now, we'll go over to this Wpf Applcation and we'll add some references. I need to add references to those two assemblies. I'll click on this Solutions section, inside projects, put a check-mark beside both of these projects, and then click OK, and now I have references to both of those. At this point, I have my assemblies, I have my controls. I have my consumer, the Wpf Application that has references so I can access those.
You'll remember that I registered that namespace. So, let's see how you would bring that namespace into this Window1.xaml. We'll go up here to my namespace declaration area. Give it my own prefix, then I'll type the equals sign and Visual Studio is doing something nice for me. It's looking through all of the registered URIs. Any namespace that was registered using that attribute I just showed you, and it's listing them all here. It's also looking through all of the namespaces in all the referenced libraries and showing those.
This is quite a long list. Here's all the predefined URIs, at the top of this section, and then there's all the .net namespaces and other libraries here. You'll notice that the URI that I used is at the top of this list. There it is. So we'll choose that one. I get to pick my own prefix, so I chose the word 'course'. Now I come down here to my grid and bring in my IpAddress. There it is, bring that over so you can see it. My IpAddress, I'll bring in the gauge.
It has a property called degree. So, I'll set that to 15 degrees and you'll see that the gauge changes. This will also work with my Octopod and the background. I'll show you what I mean. First, I'll add the background. Notice when I hit the space bar here Visual Studio looks through the classes that are exposed inside those namespaces and shows them to me here. So I'll choose WaterBackground and then I'll copy this.
Paste it in, put in my Octopod and now it works like any other element. I can pick up this Octopod and drag it around to new positions, and while I'm doing that it's writing out a margin setting inside the xaml. To review what we've seen here, is that you can create your own unique URI, map it to your own set of namespaces. Those namespaces can be in multiple assemblies and by using that one, the same unique URI, I bring both of those disparate assemblies and disparate namespaces into the same xaml namespace.
- What is XAML?
- What frameworks use XAML?
- Working with XAML and Visual Studio
- Exploring XAML namespaces
- Instantiating objects
- Subscribing to events
- Using XAML in Windows Presentation Foundations, Universal Windows, and more