Working With Various Phone Tasks in Windows Phone 7


Introduction

In this article we are going to explore how to work with various phone tasks in Windows Phone 7. Further we will discuss how to initialize or use these tasks inside Windows Phone 7. There are some chooser tasks and we will discuss them also. Further in this article we will see what chooser task is useful for what purpose according to the requirements which will be seen. Further you can find all these tasks inside the Microsoft.Phone.Tasks namespace. There are many classes available there but we will use some important and useful classes for the purposes of the demonstration. It's very important to understand these tasks which are basic for the Windows Phone 7. After that, further you will be able to understand and use the other classes too. There are some names of phone tasks which will be discussed later in this article. If you want to implement such tasks then you have to use the steps given below. Here are some important tasks which are given below.

  • AddressChooserTask It is one of the important tasks which allows an application to launch the Contact application
  • BingMapsTask It is one of the important tasks which allows an application to launch the Bing Maps
  • CameraCaptureTask This task allows an application to launch the Camera application
  • EmailAddressChooserTask It is used to allow an application to launch the Contacts application
  • EmailComposeTask It allows an application to launch the Email application
  • PhoneCallTask It allows an application to launch the Phone application
  • PhoneNumberChooserTask It allows an application to launch the Phone Contact application
  • PhotoChooserTask It allows an application to launch the Photo Chooser application
  • SearchTask It allows an application to launch the Web Search application
  • SmsComposeTask It is used to launch the Messaging application to compose a new SMS
  • WebBrowserTask It allows an application to launch the Web Browser application

Step 1: In this step first of all we have to open a Windows Phone application; let us see how you will open it.

  • Go to Visual Studio 2010
  • File->New->Project
  • Select the template named as Silverlight for Windows Phone
  • Select the Windows Phone application
  • Give it a name as you prefer.

Step_1_1fig.gif

Step_1_2fig.gif

Step 2: In this step we will see that there is an important namespace which you have to add to the MainPage.xaml.cs file; let us see the namespace which is given below.

Using Microsoft.Windows.Phone.Tasks;

Step 3: In this step we will see the MainPage.xaml.cs file code for every type of task which we are using given below. Each task code is for the click event of a button.

Code:

using
System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Controls.Maps;
namespace various_task

{

    public partial class MainPage : PhoneApplicationPage

    {

        // Constructor

        public MainPage()

        {

            InitializeComponent();

        }
        private void Myaddress_task_Click(object sender, RoutedEventArgs e)

        {

            AddressChooserTask MyACT = new AddressChooserTask();

            MyACT.Show();
        }
        private void Mywebbrowser_task_Click(object sender, RoutedEventArgs e)

        {

            WebBrowserTask MyWBT = new WebBrowserTask();

            MyWBT.Uri = new Uri("http://www.c-sharpcorner.com/", UriKind.Absolute);

            MyWBT.Show();

        }
        private void Mybingmap_task_Click(object sender, RoutedEventArgs e)

        {

            BingMapsTask MyBMT = new BingMapsTask();

            MyBMT.SearchTerm = "Delhi";

            MyBMT.Show();

        }
        private void Myemailchooser_task_Click(object sender, RoutedEventArgs e)

        {

            EmailAddressChooserTask MyEACT = new EmailAddressChooserTask();

            MyEACT.Show();
        }
        private void Myphotochooser_task_Click(object sender, RoutedEventArgs e)

        {

            PhotoChooserTask MyPCT = new PhotoChooserTask();

            MyPCT.ShowCamera = true;

            MyPCT.Show();
        }
        private void Myphonecall_task_Click(object sender, RoutedEventArgs e)

        {

            PhoneCallTask MyPCT = new PhoneCallTask();

            MyPCT.DisplayName = "Amit Maheshwari";

            MyPCT.PhoneNumber = "9412331585";

            MyPCT.Show();
        }

    }

}

Step 4: In this step we will see the Design code for the MainPage.xaml file which is given below.

Code:

<phone:PhoneApplicationPage

    x:Class="various_task.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock x:Name="PageTitle" Text="Various Chooser Task" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontFamily="Comic Sans MS" FontSize="40">

                <TextBlock.Foreground>

                  <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                    <GradientStop Color="Black" Offset="0" />

                    <GradientStop Color="#FF1FFFFF" Offset="1" />

                  </LinearGradientBrush>

                </TextBlock.Foreground>

            </TextBlock>

        </StackPanel>

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Grid.Background>

                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                    <GradientStop Color="Black" Offset="0" />

                    <GradientStop Color="#FFC4B197" Offset="1" />

                </LinearGradientBrush>

            </Grid.Background>

            <Button Content="Click_AddressChooser_task" Height="92" HorizontalAlignment="Left" Margin="12,28,0,0" Name="Myaddress_task"

                    VerticalAlignment="Top" Width="387" Click="Myaddress_task_Click" FontFamily="Comic Sans MS" FontSize="26">

                <Button.Background>

                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">

                        <GradientStop Color="Black" Offset="0" />

                        <GradientStop Color="#E59BCBCB" Offset="1" />

                    </LinearGradientBrush>

                </Button.Background>

            </Button>

            <Button Content="Click_WebBrowser_task" Height="83" HorizontalAlignment="Left" Margin="12,128,0,0" Name="Mywebbrowser_task"

                    VerticalAlignment="Top" Width="387" Click="Mywebbrowser_task_Click" FontFamily="Comic Sans MS" FontSize="26">

                <Button.Background>

                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">

                        <GradientStop Color="Black" Offset="0" />

                        <GradientStop Color="#FFF2CECE" Offset="1" />

                    </LinearGradientBrush>

                </Button.Background>

            </Button>

            <Button Content="Click_BingMap_task" Height="83" HorizontalAlignment="Left" Margin="12,218,0,0" Name="Mybingmap_task"

                    VerticalAlignment="Top" Width="387" Click="Mybingmap_task_Click" FontFamily="Comic Sans MS" FontSize="26">

                <Button.Background>

                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                        <GradientStop Color="Black" Offset="0" />

                        <GradientStop Color="White" Offset="1" />

                    </LinearGradientBrush>

                </Button.Background>

            </Button>

            <Button Content="Email_AddressChooser_task" Height="83" HorizontalAlignment="Left" Margin="12,308,0,0" Name="Myemailchooser_task"

                    VerticalAlignment="Top" Width="387" Click="Myemailchooser_task_Click" FontFamily="Comic Sans MS" FontSize="26">

                <Button.Background>

                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                        <GradientStop Color="Black" Offset="0" />

                        <GradientStop Color="#FF9094BC" Offset="1" />

                    </LinearGradientBrush>

                </Button.Background>

            </Button>

            <Button Content="Click_PhotoChooser_task" Height="76" HorizontalAlignment="Left" Margin="12,398,0,0" Name="Myphotochooser_task"

                    VerticalAlignment="Top" Width="387" Click="Myphotochooser_task_Click" FontFamily="Comic Sans MS" FontSize="26">

                <Button.Background>

                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">

                        <GradientStop Color="Black" Offset="0" />

                        <GradientStop Color="#FF20536C" Offset="1" />

                    </LinearGradientBrush>

                </Button.Background>

            </Button>

            <Button Content="Click_PhoneCall_task" Height="75" HorizontalAlignment="Right" Margin="0,482,57,0" Name="Myphonecall_task"

                    VerticalAlignment="Top" Width="387" Click="Myphonecall_task_Click" FontFamily="Comic Sans MS" FontSize="26">

                <Button.Background>

                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">

                        <GradientStop Color="Black" Offset="0" />

                        <GradientStop Color="#D787FFFF" Offset="0.854" />

                    </LinearGradientBrush>

                </Button.Background>

            </Button>

        </Grid>

    </Grid>

</phone:PhoneApplicationPage>

Step 5: In this step we will see the design of the MainPage.Xaml file which is given below.

Designimg.gif

Step 6: In this step we are going to run the application; the related output is given below.

Output 1: Whenever you click on the first button then it will open up the contact details in your Phone device as shown below.

Output2.gif

Output 2: Whenever you click on the second button then it will launch the browser with the specified URL as shown below.

Output3.gif

Output 3: Whenever you click on the third button then it will open the result shown below:

Output_3_2.gif

Output 4: Whenever you click on the Fourth button then it will show the following output.

Output4.gif

Output 5: Whenever you click on the fifth button then it will show the window which is given below.

output5.gif

Output 6: In this the first screenshot shows the number with the display name and asks the user to confirm whether he really wants to call the person. Based on user input, it pops up the second screen where you will see the actual call happening to the person.

Output6.gif    Output_6_2fig.gif

Here are some other resources which may help you.

Task System in Windows Phone 7
How to work with a WrapPanel in Windows Phone 7
Code to select photo on Windows Phone 7
Code to Search in Windows Phone 7
Working With ConnectionSettings Task in Windows Phone


Similar Articles