Viewers: in countries Watching now:
Microsoft Silverlight 5 is a rich application framework for creating high-performance, cross-platform desktop and mobile applications. In this course, author Walt Ritscher demonstrates how to build a variety of applications in Silverlight, with particular focus on building compelling business applications and delivering premium video and audio content. Developers will work with the C# programming language and Visual Studio Professional, as well as Expression Blend, a tool that simplifies creation of the interactive user interfaces expected in modern-day applications.
In this movie, I will show you how to subscribe to an event in Silverlight. I'm inside Visual Studio and I've opened a project called WiringUpEventHandlers. I'm going to start by looking inside the InXaml.xaml file. For this first demo, I'm going to show you how to do XAML-only wire up of event handlers. I'm going to start by adding an event procedure to this button. This button does not have a name and that will be significant when I enter my event handler. I'm going to use the click event handler, so I'll come here and I'll type in the word click.
If you look at the IntelliSense, you'll see that there's a lightning bullet on the left-hand side. That lightning bullet signifies that this is an event member of the Button class. I'm going to press Tab to finish typing the word click and Visual Studio says, would you like a new event handler? I can press Tab a second time to stub in the code. Name of my event handler is Button_Click. But it doesn't work too well when you have more than one button, it's better if you name your button. My second button has a name.
It's called LiveButton. Let's go through those same steps. Click, Tab, I'm going to choose the first option here New Event Handler. Now, when Visual Studio stubs in the code it uses the name of my element to generate the name of the function. So, now it's called LiveButton_Click. There are hundreds of events available in Silverlight. I thought I would show you an event for the Ellipse shape. Let's scroll down to the Ellipse and I'll type in mou and we'd look at some of the mouse events here.
I've got MouseEnter and MouseLeave and MouseLeftButtonUp is the one I'm interested in. Now, I'm going to press Tab two times, and again it stubs in the code. I do have to write some code in these event handlers. To do that I'm going to press the F7 key to switch to my code behind and over here you can see that Visual Studio has stubbed in three different event procedures. The first one is Button_Click, the second one is LiveButton_Click, and of course, here's my Mouse event handler. They look similar; however, if you look closely at the MouseLeftButtonUp, you'll see that the second argument is MouseButtonEventArgs where it is RoutedEventArgs in the other two event handlers.
You'll learn more about that later. I'll write a little bit of code here. resultTextBlock.text. This is a UI element that I'm going to output some text into, equal (=) and then the name of my control, a close one Button 1. I'll Copy this code and paste it in my other two procedures and then change the strings, and now I'm ready to test my application.
What should happen is when I click on the buttons I want a mouseup on the shape; I should see some text output to my UI. I'll press F5 to run the application. Click on the first button, see this successful output; the second button. And now I'm going to click on the ellipse. I'm clicking on the ellipse but I haven't let go off the mouse button yet. It's not until I let go, right now, that you see the text output happen. Returning back to Visual Studio, there is another way to add an event procedure to your XAML, that is, with the Property window.
I'll scroll down, select my Ellipse, switchover to the Property window, and click on the Events tab. These are all the events that are available for the ellipse, and as I scroll down the list, you'll see at the MouseLeftButtonUp level, the name of the function that was already created for me. If I want to add another event, there're two ways I can do that. I can double-click, say here in MouseMove, and Visual Studio again will stub in some code on my behalf; or I can come over here, let's go the MouseWheel, and I can type in the name of a function. ABC_Wheel.
Now, rather than relying on Microsoft's naming conventions, I'm choosing the name of the function that I want. And I'm going to press Enter and again you see it stubs it in, but this time with my name. Next, I'm going to show you how to how to wire up event handlers in code behind. So, I'm going to switchover to this XAML file, which has a very similar UI to the one I just showed you. Then I'm going to switch to code behind. This is one way to wire up an event handler in code. This is my shape; this is the event that's going to be raised in the shape.
I then use the C#+= syntax to signify that I'm registering an event handler and then I use the name of the function. Of course, I have to have that function somewhere in my code. Here it is and this function has to have the correct signature. By that I mean, it has a return of void; and have two arguments, the first argument being an object, and the second argument being MouseButtonEventArgs. The more verbose way of wiring up the event handler is shown on this line. It starts off the same but after the += symbol, it says new MouseButtonEventHandler and then in parentheses it has the name of the function.
This is the more verbose syntax. What this is signifying is that I'm initiating an instance of the delegate that's going to communicate between my code and the object raising the event. For my third example, I'm going to show you how to do it using their RoutedEvent syntax. RoutedEvents are actually managed by the dependency system, so this is the official way to add a handler in the dependency system way. I'll use the AddHandler function name and then a couple of arguments are important here; the name of the event and the function that I'm going to call.
What I would you to take away from this movie is that there are two ways to wire up your event handlers. You can do it in your XAML file or in your code behind file, plus Visual Studio provides tools to help you write the code.
Find answers to the most frequently asked questions about Silverlight 5 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.