Tasks

Tasks Integrate into default Windows Phone 7 functionality. In this tutorial we cover starting a phone call, beginning SMS and email messages, searching for a location and opening the web browser.

Setup

Before jumping into the tutorials head to developer.windowsphone.com to download the free tools and get them installed.

iPhone applications use a method named openUrl to open external applications, in Windows Phone 7 external integration is done via Launchers, Choosers and Tasks. These all live in the Microsoft.Phone.Tasks namespace of the Microsoft.Phone assembly. For this application we’ll be looking at a few of the Tasks, I highly recommend browsing through the namespace to see what’s available to you. There is however some glaring ommissions in my opinion.

After setting up your new project we’ll adding five buttons to the UI, the great thing with WP7 is that the default application styles lays them out perfectly to the UI guidelines. We’ll also attach some methods to the button’s click events. In the end the xaml should look like:

<Grid x:Name="ContentGrid" Grid.Row="1">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Button Content="Open Phone" Click="OnOpenPhone"/>

    <Button Content="Open SMS" Grid.Row="1" Click="OnOpenSMS"/>

    <Button Content="Open Email" Grid.Row="2" Click="OnOpenEmail"/>

    <Button Content="Open Map" Grid.Row="3" Click="OnOpenMap"/>

    <Button Content="Open Browser" Grid.Row="4" Click="OnOpenBrowser"/>
</Grid>

Using each task is incredibly simple, we create the appropriate task object, populate some details and call the Show method. One thing that stands out very quickly is that there’s no way to open the Maps application directly, in the end the easiest way to get to the Maps application is to search for an address, the search results will then contain a link to the Maps application for that address.

public partial class MainPage

{

    public MainPage()
    {
        InitializeComponent();
    }

    private void OnOpenPhone(object sender, RoutedEventArgs e)
    {
        var phoneCallTask = new PhoneCallTask
        {
            DisplayName = "Nigel Sampson",
            PhoneNumber = "021 555 5555"
        };

        phoneCallTask.Show();
    }

    private void OnOpenSMS(object sender, RoutedEventArgs e)
    {
        var smsComposeTask = new SmsComposeTask
        {
            To = "021 555 555",
            Body = "Windows Phone 7 SMS"
        };

        smsComposeTask.Show();
    }

    private void OnOpenEmail(object sender, RoutedEventArgs e)
    {
        var emailComposeTask = new EmailComposeTask
        {
            To = "[email protected]",
            Subject = "Windows Phone 7 Email",
            Body = "Looking good!"
        };

        emailComposeTask.Show();
    }

    private void OnOpenMap(object sender, RoutedEventArgs e)
    {
        var searchTask = new SearchTask
        {
            SearchQuery = "46 Anglesea Street, Auckland 1011, New Zealand"
        };

        searchTask.Show();
    }

    private void OnOpenBrowser(object sender, RoutedEventArgs e)
    {
        var webBrowserTask = new WebBrowserTask
        {
            URL = "http://compiledexperience.com/blog"
        };

        webBrowserTask.Show();
    }
}

Download the Code

The code for all the tutorials is available to download: Windows Phone 7 Tutorials Solution.