How To Launch Search Task and Email Compose Task in Windows Phone

Introduction

Here we will build a demo application to implement Search and Email Compose tasks.

Procedures

Step 1

First of all create a new “Silverlight Windows Phone Application” in Visual Studio and name it as you prefer (I named it "SearchandEmailComposeTask").

Now a new Windows Phone Application Page (MainPage.xaml) will be generated.

Add four TextBox controls and two Button controls to your project.

(Name them as you want or leave them as they are).

//The Xaml Code for MainPage.xaml will look like this(for a Grid)

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

            <TextBox Height="69" HorizontalAlignment="Left" Margin="12,42,0,0" Name="searchTextBox" Text="" VerticalAlignment="Top" Width="424" />

            <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,15,0,0" Name="textBlock1" Text="Enter Text to Search" VerticalAlignment="Top" />

            <Button Content="Search" Height="72" HorizontalAlignment="Left" Margin="53,138,0,0" Name="searchButton" VerticalAlignment="Top" Width="331
            "
 Click="searchButton_Click" />

            <TextBlock Height="30" HorizontalAlignment="Left" Margin="24,253,0,0" Name="textBlock2" Text="Email Tasks" VerticalAlignment="Top" />

            <TextBox Height="71" HorizontalAlignment="Left" Margin="96,289,0,0" Name="emailToTextBox" Text="" VerticalAlignment="Top" Width="340" />

            <TextBox Height="71" HorizontalAlignment="Left" Margin="96,366,0,0" Name="emailCcTextBox" Text="" VerticalAlignment="Top" Width="340" />

            <TextBox Height="71" HorizontalAlignment="Left" Margin="96,443,0,0" Name="emailSubTextBox" Text="" VerticalAlignment="Top" Width="340" />

            <TextBlock Height="30" HorizontalAlignment="Left" Margin="27,307,0,0" Name="textBlock3" Text="To:" VerticalAlignment="Top" />

            <TextBlock Height="30" HorizontalAlignment="Left" Margin="28,384,0,0" Name="textBlock4" Text="Cc:" VerticalAlignment="Top" />

            <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,463,0,0" Name="textBlock5" Text="Subject:" VerticalAlignment="Top" />

            <Button Content="Mail" Height="72" HorizontalAlignment="Left" Margin="53,520,0,0" Name="mailButton" VerticalAlignment="Top" Width="368"
            Click
="mailButton_Click" />

        </Grid>

The MainPage will look like this:

Search Task and Email Compose Task in Windows Phone

Step 2

Now go to the MainPage.xaml.cs file and add a using reference for the Phone task as in the following:

using Microsoft.Phone.Tasks;

Step 3

private void searchButton_Click(object sender, RoutedEventArgs e)

        {

            //Creating a reference for SearchTask class

            SearchTask search = new SearchTask();

            search.SearchQuery = searchTextBox.Text;

            //Show The Search Task 

            search.Show();

        }

Here in this code we are just creating a reference for the SearchTask class name "search". It has a "SearchQuery" property for the item to be searched. Here we are using the text from the TextBox and assign it to the "SearchQuery" property.

Then we just call the "Show()" method that will launch the Search Task. And that's it, a search task is launched.

Step 4

Now we will deal with the EmailCompose task; for this just navigate to "Email Button Event Handler".

And add the following code:

  private void mailButton_Click(object sender, RoutedEventArgs e)

        {

            //Creating a reference for EmailComposeTask class

            EmailComposeTask email = new EmailComposeTask();

 

            email.To = emailToTextBox.Text;

            email.Cc = emailCcTextBox.Text;

            email.Subject = emailSubTextBox.Text;

            //Show The Email task

            email.Show();

        }

Here we create a reference for the EmailCompose task class named "email". It has a "To" property for the email id to be sent, also "Cc" that takes another email id and the "Subject" property.

Now the "Show()" method is called that will launch an "EmailCompose" task.

That's all for this article. I am embedding the source file so that you can go through it.

Thank you.
 

Up Next
    Ebook Download
    View all
    Learn
    View all