A .NET type can define an event. Other .NET objects can subscribe to the event, by creating a .NET method with the correct signature and indicating to .NET that they are a subscriber. This tutorial looks at event attributes and show how to add them to an object element. You’ll see the syntax for subscribing to an event and indicating the name of the C# event handler.
- [Instructor] You'll see multiple XAML namespaces declared in XAML files. Here's what each one provides for my XAML file. I've selected them at the top of this file. This is a WPF Window file, and these are the standard namespaces you get when you create a new file. There's five of them. First one has no prefix. Second one has x prefix, and they have a d prefix, mc, and local. This top one I've talked about in several other movies in this chapter. It's the default namespace, and it's the one that allows me to map these elements to .NET types and these attributes to .NET properties.
Next on the list is one called the XAML namespace. Now, these are all XAML namespaces, but this is the one specific to the XAML parser. It contains XAML language items, ones that make sense to the XAML parser and not specifically tied to WPF or Silverlight or other XAML flavors. Here's how you might use it. In this Button element, I'd like to set the background color, background brush, to a Null reference. If I was doing this in C Sharp, I would assign a null reference to the background property, and I would do that by using a null keyword, the C Sharp null keyword.
It's not possible for me to work with that in the default namespace. Think about this for a second. This is for mapping .NET types to elements and .NET properties and events to attributes. Assigning a null reference is using the C Sharp keyword, so there is no way to do that mapping. So, instead, we put that in this namespace, and then I can use x:Null. They get that Null reference value. This third one uses the d prefix. This is intended for designer support, specifically designer support in the XAML design surfaces of Visual Studio.
I'll open up this MainNamespace.xaml file, and I get the designer here. So this designer, we can instruct that designer to do certain things or use certain values by using the d prefix. Here's an example. Typically, in WPF development, you want to have a flexible UI. A lot of times, we don't assign widths and heights to things because we like them auto sized based on what's happening at runtime. But when you're working with the designer, if you don't set sizes to items, sometimes you can't see them.
They might be too small, so what I can use is use this d prefix and use d:DesignWidth and d:DesignHeight to specify values, and these are only used by the designer. They only affect the design aspects of how XAML behaves. These attributes are ignored when the XAML is loaded by the XAML parser and the application runs. Now, to make this actually work, we also have to work with this prefix, mc. This is called the markup compatibility mode, and here's the key thing that it's used for.
We say mc:Ignorable equal d, where d represents this namespace. This technique enables the runtime XAML parsers to ignore the design attribute, as I previously described. This allows you to specify information used by the designer at design time, which is ignored at runtime. This final namespace is when you want to have your own custom namespaces. So the way you read this is we're using a clr namespace that's declared somewhere in a .NET assembly, and the name of that namespace is UnderstandNamespaces, and I've aliased that to this prefix.
So over here you can see I've got this TourData class. It's inside that namespace, and it has three properties: TourName, StartDate, and City. So now I can go to my XAML, this XAML file, and I can instantiate my TourData class. So I'm gonna say local:TourData, and then I give it a TourName, and then if I come over here, you'll see I also have StartDate, and I have City. Each one of these attributes is very useful.
As I go through the rest of this chapter, I'll show you how to do more with local namespaces, creating your own ad hoc namespaces and also talk about how Microsoft actually does the mapping. What do they do in their .NET assemblies to make the mapping happen?
- 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