Events attributes provide a way to register event handlers in XAML files. This tutorial looks the event attribute syntax and shows the auto-completion help for event attributes available in the Visual Studio XAML editor.
- [Narrator] In this video, I'll look at the other type of attribute available for an object element. I'm referring to the EventAttribute. It lets you indicate what Event Handler is assigned to an Event. Or, to say it another way, what C Sharp code will run when an Event is raised. Here is the 30 second overview of .NET Events. 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 method is called an Event Handler.
When the Event is raised, .NET calls all of the registered Event Handlers. Each Event defines a specific set of parameters. The Event Handler must exactly match that parameter list. This is how information is passed from the Event Publisher to the Event Subscriber. What we are doing with Zamil EventAttributes is subscribing to an Event and indicating the name of the C Sharp Event Handler. To demonstrate this, I'll use this UI. Consists of a TextBlock and a TextBox, a ListBox with a list of items, two Buttons, and a Rectangle.
I'll add an EventAttribute to each one of these. I'll start with the TextBlock, put my cursor within the boundaries of the TextBlock, and press the space bar. Visual Studio knows that I'm inside the TextBlock, so it gives me a list of potential attributes. You can tell that it's a property attribute by looking at the wrench symbol here. You can tell that it's an EventAttribute by looking at the lightning bolt. I'll use this ToolTipOpening. I'll select it with my mouse. This Event fires just prior to the ToolTip showing up on the user screen.
In this case, the user would hover over the TextBlock, the ToolTip gets ready to launch. My code gets executed. And that allows me to intercept the code and change the text on the ToolTip. I'll press the tab key. Visual studio finishes typing the word ToolTip and puts the equal sign in. And then it's suggesting something called a New Event Handler. So, I'll press the tab key a second time, and Visual Studio stubs in the code. So, let's review what's happening here. This is the name of the Event, that's inside the TextBlock class.
This is the name of the C Sharp method that will get called. This is the registered Event Handler. Look at the name. I had given my TextBlock a name, so when Visual Studio stubbed in the code, it used my name, _ToolTipOpening. Now go to the TextBox. I'm looking for the TextChanged, so I'll type in textc, like that. See it again. Now this time, I'll select it with my mouse. I can move up and down in this list, select the item, double click on it, and then double-click on this New Event Handler.
In this case, my element does not have a name, so it uses a generic name for the Event. TextBox_TextChanged. With the ListBox, I use SelectionChanged. This fires when you change, then the user selects a different item in the list. Double click on this, and then double click on this to finish stubbing in the code. If you ever want to look up the definition of the Event, select the item in Visual Studio, and then right click and choose go to definition.
And it shows that I'm inside the selector, which is one of the base classes for the ListBox. And it's showing me SelctionChanged. So this way I can read more about the Event and look at its parameters and get some extra information about it. Buttons are controls that the user interacts with. If you're on a WPF application, they're most likely going to be interacting with the button with a mouse, but they could also use touch in some cases, or a stylus, or a pen. But when you're working with a phone, they're more likely to be using a touch gesture.
And in our case, it's called click. That's the Event we're looking at. I'll type in click, tab twice, and what I want you to see now is when I go down to this next button, and do the same action, I get a different list this time. Visual Studio looks through my code-behind and seeing that there's already a method in my C Sharp file that has the exact signature for Click Handler. So, now it's offering two choices. Either put in a New Event Handler or use the existing Event Handler. So, I choose the existing Event Handler, like this.
Now, think what's happening. I've got two instances of the Button, Button one and Button two, and when the event's raised on this Button, it's going to be passed into this Event Handler. When the event is raised on this other Button instance, it's going to get passed to the same Event Handler. So, I've got two separate Buttons, both funneling their code into that same Event Handler. This could mean we have to differentiate in our code and ask .NET, which of the two Buttons raised the event.
Finally, for this Rectangle, I'll look at the MouseMove Event. At this point, I've got all my EventAttributes in the code. As you can see, it's pretty straight forward. Visual Studio stubs in the code, but we still need to write that code, and I'll show you how to do that next. There are other ways to react to user interaction without using Events. I can think of two: behaviors and commands. Behaviors provide some benefit, mostly in enabling designers to wire up behaviors without having to write code or ask a developer to create the code.
Commands aid in decoupling the Zamil and code-behind file. You'll see them used in the model view, view model pattern, a popular pattern used in Zamil based applications. Those are topics for another day. Let's look at how to write the C Sharp code for the Events.
- 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