Creating a Simple Action - Page 1

by kirupa  |  13 October 2010

  Have questions? Discuss this Windows Phone tutorial with others on the forums.

In a few previous tutorials, you learned all about Behaviors and how to use a behavior that is provided out of the box by Expression Blend. You find a behavior in your Assets Library, drag/drop it onto something, tweak a few properties, and (like magic!) you are done and everything just works.

There will be times, though, where you will not find a built-in behavior to suit what you are trying to do:

[ what, you want fries with that as well? ]

In those dark, uncertain times, you will basically need to write your own behavior. Fortunately, as you will see shortly, that isn't something that is too difficult. The Behaviors functionality is made up of three components - Behavior, Action, and Trigger. Each component does something pretty unique, so this tutorial is focused on the most common of them, the Action.

What is an Action?
Mundane things you face in in real life and mundane things your applications face, in many ways, are not that much different. They all revolve around cause and effect. When I flip on a light switch, the light turns on. When I click on a button, a sound plays. When my application hits a certain time, it needs to close.

You can model those types of relationships very simply with the following sentence:

trigger_action

There are two behavior components that represent the cause and effect portion of the sentence you see above. The first is the Trigger, and the second is the Action:

Like two socially awkward best friends, a Trigger and Action are always together and within earshot of each other. The Trigger is what determines when your Action should fire. Your Action is what actually does something. Independently, they are not useful, but together, they make up the bulk of what you will model using behaviors.

In this tutorial, you will learn how to create an Action that works with our built-in Triggers to display a message box. Let's get started.

Getting Started
While this tutorial is written with Windows Phone development in mind, the contents of this tutorial are equally applicable to Silverlight and WPF project. So, make sure you have everything up and running by looking at the Getting Started with Expression Blend article.

Creating the Action
Launch Expression Blend and create new project. Like I mentioned earlier, your project can be either Windows Phone, WPF, or Silverlight. I will be working with a Windows Phone project.

Once your project has been created, take a look at your Projects panel:

[ your Projects Panel displays files currently in your project ]

Right click on the node representing your project (WindowsPhoneApplication10 in my case) and select Add New Item:

[ we are going to add an Action - this is so exciting!!!! ]

The Add New Item dialog will appear. You will see a list of item types you can create. Select the entry for Action and give it the name ShowMessageBoxAction.cs in the Name field:

[ select the Action item from the New Item dialog and give it a Name ]

Once you selected the Action entry and gave it the ShowMessageBoxAction name, click OK to close this dialog and to add your new Action item to your project. A few seconds later, you'll see a CS file called ShowMessageBoxAction created in your project and open for you to edit:

[ your Action has been created! ]

Ok, now that you have created your Action, let's go ahead and add some code to make it do something. We will do that and more on the next page.

Onwards to the next page!


 1 | 2




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.