Thursday, May 17, 2012    
Blog  

OpenLight Blog

Getting the right behaviors in your Phone 7 App – Part 1 Phone Home

Dec 21

Written by:
12/21/2010 1:01 AM  RssIcon

This multiple part series will focus on packaging up a few common “tasks” into reusable behaviors. I hope to cover tasks for phone calls, sending emails, launching the browser etc. Most of the articles in this series will be short and typically will contain only a few lines of code. The majority of the code will deal with the objects contained in the Microsoft.Phone.Tasks namespace which contains several classes used to launch a task or choose a piece of data from the phone.

In this post (part 1) will we tackle what I consider to be the most obvious task to use on a phone… making a phone call. Again this will require only a few lines of code, so I will go ahead and drop it all right here and then run through what it does and how to use it.

using Microsoft.Phone.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace ITLackey.Phone7.Common.Behaviors
{
     public class LaunchPhoneDialerAction : TriggerAction<Control>
    {
        public LaunchPhoneDialerAction()
        {
           
        }
        protected override void Invoke(object o)
        {
            PhoneCallTask task = new PhoneCallTask();
            
	   task.PhoneNumber = this.AssociatedObject.Tag.ToString(); 
            
            task.Show();
        }
    }
}

 

What we have here is a standard “Action” (as you would see in any Silverlight application) that can be dropped onto any object that derives from the Control base class (For a refresher on behaviors check out this article by Michael http://openlightgroup.net/Blog/tabid/58/EntryId/17/Creating-a-simple-Silverlight-3-Behavior.aspx and now there is a handy template available in Blend).  The real work for this example is done inside of the Invoke method and by the majority of work I am referring to the three lines of code.

The Phone 7 team has done a fine job providing and easy way to launch the phone dialer prepopulated with a phone number. In this example, we are pulling the phone number out of the Tag property of the control this action is associated with. This allows us to bind the Tag property to some dynamically loaded data so that this action could be used inside of a ListBox ItemTemplate for example. That would look something like this:

<ListBox x:Name="listBox">
	<ListBox.ItemTemplate>
		<DataTemplate>
			<HyperlinkButton Tag="{Binding PhoneNumber}" Content=”{Binding PhoneNumber}”>
				<Custom:Interaction.Triggers>
					<Custom:EventTrigger EventName="Click">
						<ITLackey_Phone7_Common_Behaviors:LaunchPhoneDialerAction/>
					</Custom:EventTrigger>
				</Custom:Interaction.Triggers>
			</HyperlinkButton>
		</DataTemplate>
	</ListBox.ItemTemplate>
</ListBox>

This would only display a list of phone numbers, obviously you would typically want to include a more useful item template, but hopefully this illustrates the point. Anytime you click on one of the phone numbers the phone dialer will launch preloaded with that phone number.

Well there you have it, a reusable phone dialer launching action that can be dropped onto any control in your phone app. Unfortunately, sine Phone 7 uses Silverlight 3 we cannot bind the phone number data directly to a property on the action due to the lack of support in Silverlight 3 to bind to objects that do not inherit from FrameworkElement. (This article shows the announcement of this feature being added to Silverlight 4 http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#dobind  ). This is why we have to use the Tag property of the associated object. I feel that is a small price to pay for the reusability  of this commonly used action.

As always any feedback, improvements, questions etc. are appreciated!

Tags:
Categories:

4 comment(s) so far...


Gravatar

Re: Getting the right behaviors in your Phone 7 App – Part 1 Phone Home

Great Start !!!

Thanks for all the write up. Pretty interesting info.

Regards

KRK

By krk on   12/21/2010 9:10 AM
Gravatar

Re: Getting the right behaviors in your Phone 7 App – Part 1 Phone Home

Cool thanks, glad you are enjoying it! I should have a few more in the next couple of days.

By Ian T. Lackey on   12/21/2010 9:13 AM
Gravatar

Re: Getting the right behaviors in your Phone 7 App – Part 1 Phone Home

This is VERY helpful. I just used code from the article you did on the back button last night.

Keep it up! Please :)

By Michael Washington on   12/21/2010 4:36 PM
Gravatar

Re: Getting the right behaviors in your Phone 7 App – Part 1 Phone Home

I plan on it, these little snippets I learned from the trenches. If I can help other avoid the shell shock then its worth it. :)

By Ian T. Lackey on   12/21/2010 4:47 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
  
Copyright 2009 by OpenLightGroup.net   |  Privacy Statement  |  Terms Of Use