In this tutorial we are going to start with the list of chooser task, in my previous tutorials we have seen the different Launcher tasks available and we explored each one of them. Here we are going to see about the Address Chooser Task, with this chooser task we can provide an option for the end user to select a contact and get information on that particular contact. This task first launches the Contacts application and then provides an option for the user to select a contact from the list of contacts available, once the user selects a particular contact an event is triggered with the selected contact detail which can be used with in the application as per the requirement. Let us see the steps on how to achieve this task in real time for a Windows phone application. [more]
Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to trigger the Address Chooser Task as shown in the screen below.
Now drag and drop few controls to the screen which is used to trigger event that launches the Address Chooser Task. Once we designed the screen with the controls we can see the screen looks like below. We have added a button control to trigger the event and text block to show the address.
Now we need to write our code in the button click event to trigger the Address Chooser task on the user click to select the contact. To do that just go to the code behind and first add the USING handler code on top with the existing using statements as shown in the code below.
using Microsoft.Phone.Tasks;
Next is to add the below code to the button click event which basically creates an instance of the Address Chooser task while the page is loading, on the Address chooser task completed we have written a code which basically gets the address of the selected contact as shown in the code below.
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;
namespace F5debugHowto20
{
public partial class MainPage : PhoneApplicationPage
{
AddressChooserTask addressTask;
// Constructor
public MainPage()
{
InitializeComponent();
addressTask = new AddressChooserTask();
addressTask.Completed += new EventHandler<AddressResult>(addressTask_Completed);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
addressTask.Show();
}
void addressTask_Completed(object sender, AddressResult e)
{
if (e.TaskResult == TaskResult.OK)
{
textBlock1.Text = “Select Contact = ” + e.DisplayName.ToString() + ” and Address is ” + e.Address.ToString();
}
}
}
}
If we can see we are using the AddressResult object where we have the selected contact name and address which the user selects from the list of the contacts from the device. addressTask_Completed is the event that is triggered once the user selects the contact name from the address chooser task which is triggered on button click event after the user selects the contact.
Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.
Output Screens:
Address Chooser task is supported in Windows Phone version 7.1 only and not with 7.0. As per the MSDN Documentation we need to take care of enabling and disabling the required component as per the needs. “To ensure that your application receives the result of the AddressChooserTask, the object must be declared with class scope within the PhoneApplicationPage class and you must call the chooser constructor and assign the Completed event delegate within the page’s constructor. For more information about how to handle the activation and deactivation of your application, see Execution Model for Windows Phone.”
That’s it from this short tutorial on Windows Phone see you all in the next tutorial soon. Mean while Happy Programming!!!
No Comments
First off I would like to say superb blog! I had a quick question that I'd like to ask if you do not mind. I was interested to find out how you center yourself and clear your head before writing. I have had a hard time clearing my mind in getting my ideas out. I truly do take pleasure in writing but it just seems like the first 10 to 15 minutes are usually wasted simply just trying to figure out how to begin. Any recommendations or hints? Thanks! <a href=http://acne.adsuse.com>what is acne</a>