Condividi tramite


Writing code that will respond to events

Blend for Visual Studio 2012 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, 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.

Creating interactivity in your Blend application

You can use event handlers to make your application respond to user input and application changes. Event handlers are set up in the Events panel. (In the Properties panel, click EventsJJ171008.6c67bb3b-e8a2-4a63-bad5-54d5c15b04dd(it-it,VS.110).png.)

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.

Hooking up to event handler methods

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, Blend will open your project in Visual Studio, 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, 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.

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 Blend. For more information, see Hook up to an existing event handler method.

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.