Compartilhar via


Event handling overview

This page applies to WPF and Silverlight 2

Microsoft Expression Blend is designed to develop rich Windows-based applications and user interfaces (UIs) that can interact with user input. For example, if you want your application to start an animation timeline that moves a ball when a user moves the mouse, Expression Blend has features that you can use to configure that relationship. You can also configure your application to respond to changes in the application state itself.

Note

For information about Microsoft Silverlight 1.0 event handling, see Overview of Silverlight 1.0 and Create a button that controls a storyboard in a Silverlight application. Also see Silverlight scripting and mouse events.

Creating interactivity in your Expression Blend application

You can use either of two methods in Expression Blend to make your application respond to user input and application changes:

  • Triggers   You configure triggers under Triggers in the Interaction panel. You do not have to know how to program or write XAML code to configure triggers.

    You can use a trigger to start, stop, pause, resume, remove, or go to the end of an animation timeline when the user interacts with the objects in your application, or when the states of those objects change. You can also set triggers on state changes to change property values (such as background color). You do not have to use code to configure triggers—you can do everything in Design view in Expression Blend. For more information, see Triggers overview and Animation overview in this User Guide.

    Note

    Triggers are not supported in Silverlight 2 projects.

  • Event handlers   You set up event handlers in the Events panel. (In the Properties panel, click the EventsCc294567.6c67bb3b-e8a2-4a63-bad5-54d5c15b04dd(en-us,Expression.10).png button.)

    You can use event handlers to run a procedure that involves more than setting a property or controlling an animation timeline. For example, you can add other programming logic such as setting a property on another object, loading or creating a new document, using a mathematical calculation to animate an object, and more. Event handler methods are defined in the code-behind file of your document, and written in C# or Visual Basic .NET. For example, if your document is named Window1.xaml, the event handlers are contained in the Window1.xaml.cs or Window1.xaml.vb file, depending on the programming language that you use.

For a list of events that you can hook up to, by using either triggers or event handler methods, see WPF events quick reference in this User Guide.

Cc294567.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

Hooking up to event handler methods

Expression Blend helps you write event handler methods by generating the code for empty methods. If you have Microsoft Visual Studio 2008 Standard Edition or a more recent version installed, Expression Blend will open your project in Visual Studio 2008, open the code-behind file, and then paste in the empty event handler method. If you do not have Visual Studio 2008 Standard Edition or a more recent version installed, Expression Blend copies the empty event handler method to the Clipboard so that you can manually open the code-behind file and paste the method in. For more information, see Create a new event handler method in this User Guide.

Expression Blend was designed with team development in mind. Often, software teams consist of designers who work on the UI and programmers who work on the code that runs behind the UI. You might receive a changed code-behind file from a developer who has added various event handler methods for you to hook into from the UI. You can do this in Expression Blend. For more information, see Hook up to an existing event handler method.

Cc294567.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

Programming issues

Experience with programming Windows-based applications is useful when you are writing event handlers. This is because of the Windows Presentation Foundation (WPF) threading model and the timing of UI rendering. For example, if you create an event handler method that updates the text that is displayed in a Label, performs some other calculations, and then updates the text in the same Label again before the event handler method finishes, you will see just the last update. This is because the rendering of the UI occurs at the end of the event handler method. All processing is accomplished on the same thread, so the application cannot take time out during the execution of your method to update the UI. For information about how to write WPF applications that have multiple UI updates and calculations, see the Threading Model topic in the WPF section of MSDN.

Cc294567.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top