Predefined XAML namespaces are convenient, but they are not available for all class libraries. This tutorial looks at an alternate syntax that lets you work with .NET or WinRT classes in any namespace, even when there is no predefined mapping.
- [Walt] Predefined XAML namespaces are convenient, but they're not available for all class libraries. Remember that someone has to take the time to setup the mapping to the URI and the .NET namespace. Also, custom predefined namespaces are not supported in Universal Windows applications. So that makes it harder to do the mapping in those libraries. There is an alternate syntax that lets you work with .NET or WinRT classes in any namespace even when there's no predefined mapping. To demonstrate that, I'll use this solution, AdHocNamespaces, which has two projects.
I have my WpfApp and I have a reference in here to this CoursewareControls library. Inside the CoursewareControls, I have two of my controls, the Gauge and the IpAddress. In my AssemblyInfo, you'll see that I commented out the XmlnsDefinition, so I don't have the predifined mapping anymore. I have a Reference over here, we saw that. And then what I did is I moved two of the controls from the separate assembly and put them in my WpfApp.
That makes them local classes that are inside my local assembly. So what I'll demonstrate is how to work with classes in an external assembly and how to work with classes that are in the local assembly. Go to the Windows XAML file. I go to my namespace declaration area. When I type the equal sign, Visual Studio looks through all of the predefined namespaces. I don't have one anymore so that's not gonna do me any good. But it also will show me all the other available assemblies. You see down here? It shows me that in an assembly called CoursewareControls is a namespace that has some classes in it called Courseware.Controls.
And in my MainApp, this is my WpfApp, I have a namespace called Courseware.Graphics. You can also see that in MainApp, I've got the MainApp namespace so I can work with classes in either one of those namespaces. So I'll start by looking at this one. Now I wanna see all of this on a single line so let me make the window a little bit wider. There you go. So what this is saying is I have a namespace. I wanna do a mapping to this XAML namespace prefix here. And it's a CLR namespace so it's telling it it's a .NET assembly somewhere and it has a namespace called Courseware.Controls.
Then you'll see there's a colon here and then there's a second bit of information, the assembly that contains the code. Here it says assembly=CoursewareControls. So that tells the XAML parser, when it encounters an element, to go look inside that assembly, find the class that's inside Courseware.Controls, and use it. Once you've done that, it works just the same as if it were a predefined namespace. Down here, you see that I have my Gauge and my IpAddress. I'll pick the Gauge, set the Degree property, and there's my control.
Next, I'll work with those classes that are in my local assembly. Go back up here, I'll add another declaration. This time I'm going to use this item here, Courseware.Graphics in the MainApp. You'll see that it has a shorter syntax up here. It still says clr-namespace:Courseware.Graphics, but no longer has to have the assembly= because it's inside the same assembly. Once you have the namespace declared, it works the same as the previous example.
So what this shows is that I can work with classes in any of these namespaces. Here's one more demonstration. I'll go and add one more namespace. As you look down this list, you'll start seeing all of the .NET namespaces. So I can see that inside the System assembly is Microsoft.Win32. Here is something inside mscorlib called System.Collections. What this shows you is that in XAML you can instantiate any type.
And that's what I'll be covering in the next chapter. I'll talk about new instantiate object instances using XAML elements.
- 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