Presentation is loading. Please wait.

Presentation is loading. Please wait.

Brown Bag Seminar Summer 2007

Similar presentations


Presentation on theme: "Brown Bag Seminar Summer 2007"— Presentation transcript:

1 Brown Bag Seminar Summer 2007
Actions Brown Bag Seminar Summer 2007

2 Actions – Brown Bag Seminar
Often we want an application to respond in some way when a user moves the mouse, clicks a button, presses a key, or draws with a pen. Windows Presentation Foundation has three common ways of dealing with actions: Events Commands Triggers We will look at the principles that apply to all three of these mechanisms, and then dig into the details about each one. Actions – Brown Bag Seminar

3 Actions – Brown Bag Seminar
Action Principles element composition loose coupling declarative actions Actions – Brown Bag Seminar

4 Actions – Brown Bag Seminar
Element Composition Remember the three principles of the control model Element composition rich content everywhere simple programming model Example of simple programming model Button b = new Button(); b.Content = "Click Me"; b.Click += delegate { MessageBox.Show("You clicked me!"); }; Actions – Brown Bag Seminar

5 Actions – Brown Bag Seminar
Element Composition Its not the button itself that is being clicked, but rather the elements that make up the display of the button Modified version of previous code snippet Button b = new Button(); b.Content = new Button(); b.Click += delegate { MessageBox.Show("You clicked me!"); }; Concept of routed events Actions – Brown Bag Seminar

6 Actions – Brown Bag Seminar
Element Composition Actions – Brown Bag Seminar

7 Actions – Brown Bag Seminar
Loose Coupling Semantic events (click) vs. physical events (MouseUp, MouseDown) Writing code against the Click event has two advantages We don’t tie ourselves to a specific input gesture (mouse versus keyboard) We don’t tie ourselves to a button. CheckBox, RadioButton, Button, and Hyperlink all support clicking. Code written against the Click event depends only on a component that can be clicked. This decoupling of code to the action produced allows for more flexibility in the implementation of handlers Actions – Brown Bag Seminar

8 Actions – Brown Bag Seminar
Loose Coupling Events themselves, though, suffer from a form of coupling that requires the method implementation to be of a specific signature. For example, the delegate for Button.Click is defined as follows: public delegate void RoutedEventHandler(object sender, RoutedEventArgs e); Actions – Brown Bag Seminar

9 Actions – Brown Bag Seminar
Loose Coupling One of the goals in WPF is to allow a spectrum of actions, ranging from tightly coupled physical events (like MouseUp) all the way to completely semantic notifications (like the ApplicationCommands.Close command that signals that a window should be closed). Actions – Brown Bag Seminar

10 Loose Coupling template for a window that adds chrome for closing the window Actions – Brown Bag Seminar

11 Actions – Brown Bag Seminar
Loose Coupling Loose coupling provides a complete abstraction from the action source (in this case, the button) and the action handler (in this case, the window). Leveraging this loose coupling, we could change the window style to use a totally different control and not break anything. Actions – Brown Bag Seminar

12 Actions – Brown Bag Seminar
Declarative Actions WPF is moving toward a model in which software declares its intent e.g., I want the window to close when you issue this command instead of its implementation e.g., Call Window.Close() when you click this button Events allow us to declare the target function in markup, but the handler must be implemented in code. Commands are specifically designed for declarative use, providing the best abstraction between the action source and consumer. Triggers provide probably the richest declarative support, but their lack of extensibility makes them difficult to use for complex tasks. Actions – Brown Bag Seminar

13 Actions – Brown Bag Seminar
Events In WPF, events behave exactly as they do in any other .NET class library. Each object exposes a set of events to which we can attach a listener using a delegate. There are three types of routed events: Direct Bubbling tunneling events Actions – Brown Bag Seminar

14 Actions – Brown Bag Seminar
Direct Events Direct events are simple events that fire on a single source; these are nearly identical to standard .NET events, with the exception that they are registered with the WPF routed-event system. The routed-event system is the part of WPF that is responsible for routing events through the element tree. This system is mostly hidden, with only small parts of it, like EventManager, RegisterRoutedEvent visible. Certain features in the platform (triggers, for example) require an event to be registered to be used. Actions – Brown Bag Seminar

15 Bubbling and Tunneling Events
Actions – Brown Bag Seminar

16 Bubbling and Tunneling Events
Tunneling events travel from the root of the element tree to a target element, and bubbling events do the opposite. Typically, these two types of events are paired, and the tunneling version is prefixed with Preview. Most input events (keyboard, mouse, and pen) have bubbling and tunneling versions of each event for example, MouseRightButtonDown and PreviewMouseRightButtonDown, respectively. Actions – Brown Bag Seminar

17 Actions – Brown Bag Seminar
Commands Used to deal with things more abstractly. Suppose we want to add the ability to quit We will first define the menu in the markup file: In the code behind, we can implement the event handler: Actions – Brown Bag Seminar

18 Actions – Brown Bag Seminar
Commands This works fine, but now lets also add a text section that includes a hyperlink to exit the application: We’re making a lot of assumptions about the implementation of ExitClicked. the signature is compatible with the Hyperlink.Click We now have arbitrary methods in the code behind baked into the markup file, so a graphic designer trying to build the UI for this application would have no idea what event handlers to bind to. Actions – Brown Bag Seminar

19 Actions – Brown Bag Seminar
Commands Commands exist to help solve this problem. They allow us to provide a single name to signify the action that we want. To start using commands, must do three things: define a command define the implementan of that command create a trigger for the command Actions – Brown Bag Seminar

20 Actions – Brown Bag Seminar
Commands Defining a new command: Actions – Brown Bag Seminar

21 Actions – Brown Bag Seminar
Commands To bind a menu item or hyperlink to Exit command: Actions – Brown Bag Seminar

22 Actions – Brown Bag Seminar
Commands Actions – Brown Bag Seminar

23 Actions – Brown Bag Seminar
Trigger Triggers are signaled by three things: The state of a display property (Trigger) The state of a data property (DataTrigger) An event (EventTrigger) Allthree trigger types cause a set of actions when they are signaled Actions – Brown Bag Seminar

24 Adding Triggers to Data
Actions – Brown Bag Seminar

25 Adding Triggers to Data
Actions – Brown Bag Seminar

26 Adding Triggers to Data
Actions – Brown Bag Seminar

27 Adding Triggers to Data
Actions – Brown Bag Seminar


Download ppt "Brown Bag Seminar Summer 2007"

Similar presentations


Ads by Google