This tutorial looks at the namespace mapping, the technique that designates how markup elements and attributes are backed up by specific CLR namespaces, .NET types and .NET assemblies.
- View Offline
- [Instructor] I've mentioned this in another movie, but it bears repeating. XAML files are XML, and they use the principles of XML namespaces. Here's an example. If open this MainNamespace.xaml file, this is a Window file in WPF project, and what I've done is I've removed some of the standard namespaces that are in this file. I only have two left on line two and line three. XAML namespaces use the standard XMLNS attribute to define the namespace. They use unique names via URIs.
They also can use namespace prefixes which provide a means to reference multiple namespaces from the same markup source. XAML adds one additional concept to XML namespaces, though. A XAML designates how markup elements and attributes are backed up by specific CLR namespaces, dot net types, and dot net assemblies. This is sometimes called namespace mapping. Here's how you can see the backing types for different elements. So I'll be using the items inside this namespace, the default namespace.
I've got several items, I've got a animation here. I've got some controls, and I've got a rectangle. So what I can do is pick one of these items. Let's say this rectangle. And when I hover mouse over it, I'll get a tool tip that tells me what namespace and what backing type is alias to this rectangle. Sytstem.Windows.Shape.Rectangle. So you get even more information. I can click on it and, right click, and choose Go To Definition. This is also available through F12, the keystroke F12.
So I'll click on Go To Definition. Now I get more details. It takes me over to the Object Browser and shows me which class, the rectangle class, and shows me this part of this namespace System.Windows.Shapes, and if I click on this link here, it'll tell me that that's part of Presentationframework. That's the dot net assembly. So I can get information about the type, its namespace, and its assembly. I can do the same thing with an attribute. Say I go up here to this Width property. Press F12.
It takes me to the Object Browser, shows me that part of the FrameworkElement which is a base class of that element. And it shows me that's the member of System.Windows.frameworkElement and I can drill down and find out that's also in PresentationFramework. This works for animations. It works for this binding here. This is a markup extensions, so if I click on this, press F12, I see that that is in the System.Windows.Data namespace.
And if I look at this animation, I press F12, and drill down, I see it's in System.Windows.MediaAnimation. And here's what I want you to see. That's in a different dot net assembly, PresentationCore. So what we've seen here is that this one default namespace has types that are in multiple dot net assemblies, and in multiple dot net namespaces, and there's multiple classes and properties that they are working with. Now let's go back to look at this XAML file. Now let's think about this.
All of those are available from this namespace and that means I can just use Window and Grid and TextBlock and DoubleAnimationUsingKeyFrames. I don't have to use any prefix. Think about what would happen if Microsoft had not done this aliasing. Now before I show you that, though, how many namespaces are there? Let's click on this Mapping.txt file. There's about 18 different dot net namespaces that are aliased to that single XML namespace. Now think about what does that mean? That would mean I might have to do something like when I work with this button, I might have to use Control:Button And when working with this binding, I have to do data:Binding.
And you see what that would mean? Is there would be a lot more prefixes and that would make my XML files bigger. And it would XML harder to read. We'll undo those two changes. That's what I wanted you to take away from this, is that Microsoft did all the work. What they had to do was go through and create all those dot net namespaces and then they had to alias them all to the same XML namespaces; they had to do one more thing, they had to make sure that in all those dot net namespaces, there was no duplicates.
Couldn't inadvertently have a dot net in one dot net namespace and another in alias as both to the same XML namespace. That would cause a conflict. So did they that work for us so that we have a simpler experience while we're working inside the XAML.
- 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