Welcome to Jeff Fansler's Blog

I've spent many years in the computer industry. I've worked in labs installing software, building computers, setting up networks, and teaching people how to use a mouse. I've worked as a developer building business applications and products. I've worked as a manager building teams and processes. In the end I've ventured on building a company which allows me to do a little of everything. This blog is where I talk about what I've learned during my journey

 

Windows Phone 7 Launchers and Choosers

posted on Saturday, August 07, 2010 9:39 PM | Filed Under [ WP7 ]

As promised, here’s my post on launchers and choosers.  There is a lot of information out there on this topic. I’m adding to it for my own benefit. The two sources that I pulled most of my information from were the 3 part post from Yochay located here: Part 1, Part 2, Part 3, and the WP7 Training Kit Launchers and Choosers Lab.

What are Launchers and Choosers

A pretty common scenario for a phone application is to utilize the functionality that exists on the phone.  I’m talking about features like accessing contact data, sending emails and texts, dialing a phone number, and searching the web.  All of these features are part of the phone and it’s nice to be able to access these features within your own applications.  As a 3rd party application developer you do not have access to these features directly. You have to use Microsots API’s to access them.  These API’s are called launchers and choosers. The difference between a launcher and a chooser is simply that choosers return data. The launchers and choosers are covered under the Windows.Phone.Tasks Namespace. You can see the list from the API docs.

Using Launchers

Launchers are pretty simple.  You create a new instance of the launcher you want to use, set any properties, and call the Show() method. Here’s an example:

var task = new WebBrowserTask() {URL = "http://www.ThisIsFanzoo.com"};
task.Show();

That’s really all there is to launchers.

Using Choosers

Choosers are similar but a little more difficult.  You have to create a new instance of the chooser and set any properties just like the launcher.  Unlike the launcher, you have to give your chooser a way to return data.  This is done using the choosers Completed event. I found this to be interesting.  If you remember my last post, I said that no matter what, if your application is not on the screen then it is terminated.  Well, when you show a chooser, your app isn’t on the screen.  If your app is terminated, how is the event going to fire when the chooser is done?  This is a gotcha that I encountered.  When I started writing my first sample, I created the chooser and wired up to the completed event all in my button click event handler.  It took me a while to figure out why the completed event wouldn’t fire when control returned to my application.  After playing for a while, I realized that when my application was terminated, so was the event handler. To get around this, I created my choosers in the class scope and initialized them in the page constructor.  This way, when my app is activated the constructor runs, wires up to the completed event and then WP7 magically fires that event. Here’s what the code looks like:

private PhoneNumberChooserTask _choosePhoneNumberTask;

public Choosers()
{
    InitializeComponent();
    _choosePhoneNumberTask = new PhoneNumberChooserTask();
    _choosePhoneNumberTask.Completed += new EventHandler<PhoneNumberResult>(ChoosePhoneNumberTaskCompleted);
}

Let’s also take a look at the completed event handler. In this example I am taking the chosen phone number and sending a text message with it:

private void ChoosePhoneNumberTaskCompleted(object sender, PhoneNumberResult e)
{
    new SmsComposeTask() {Body = "I Like Windows Phone 7", To = e.PhoneNumber}.Show();            
}

I hope you found this post helpful.  If you’d like to see the source code, you can get it here.


Comments

Gravatar
# re: Windows Phone 7 Launchers and Choosers
on 11/16/2012 5:36 AM
I can't wait to have my own Windows Phone 7 and share my thoughts regarding this amazing gadget. :)
Post Comment
Title *
Name *
Email
Url
Comment *  
Please add 3 and 2 and type the answer here: