在 WP7 中选择一个联系人并获取该联系人详细信息

鉴客 发布于 2012/01/23 17:37
阅读 717
收藏 0

Windows Phone Developer Tools 7.1 Beta (Mango) introduces several new launchers and choosers. From your WP7 application you can now choose an address, save a ringtone, retrieve contact details, etc. So, in this article I am going to talk about how to How to choose Contact and get Contact details in a Windows Phone 7 application.

Previously we covered all WP7 Launchers and Choosers in our  Launchers and Choosers "How to" series of posts. So now it is time to update this series with a few new posts.

NOTE: Before we begin make sure that you have installed the Windows Phone Developer Tools 7.1 Beta(Mango).

Using AddressChooserTask to choose a Contact

Generally AddressChooserTask launches the Contacts application and allows the user to select a contact. If the user completes the task, the Completed event is raised and the event handler is passed an AddressResult object which exposes a string containing the physical address and the display name for the selected contact. in order to use this task at first include the following namespace:

using Microsoft.Phone.Tasks;

Example:Here is how you can get DisplayName and Address related to the selected contact:

XAML:

<Button Content="Launch Contacts App" Click="btnContacts_Click"/>
<TextBlock x:Name="tbName" FontSize="25"/>
<TextBlock x:Name="tbAddress" FontSize="25"/>

C#:

AddressChooserTask addressTask;
private string displayName = "Andrew Hill";
 
// Constructor
public MainPage()
{
    InitializeComponent();
 
    addressTask = new AddressChooserTask();
    addressTask.Completed += new EventHandler<AddressResult>(addressTask_Completed);
}
 
private void btnContacts_Click(object sender, RoutedEventArgs e)
{
    addressTask.Show();
}
 
void addressTask_Completed(object sender, AddressResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        this.displayName = e.DisplayName;
        this.tbName.Text = "Name: " + e.DisplayName;
        this.tbAddress.Text = "Address: "+ e.Address;
    }
}

tip65-00tip65-01tip65-02tip65-03

Search for a Contact and get Contact details

With the Mango release you can now query the Contact List and retrieve information.  Using the AddressChooserTask  gives you only limited access to the contact details, so if you want to retrieve the full set of details you should use the Contacts class.  Here is how we can search for the target Contact using SearchAsync() method(Note that FilterKind determines the kind of filtering you want, it can be PhoneNumber, DisplayName, EmailAddress, etc.):

private void btnSearch_Click(object sender, RoutedEventArgs e)
{
    Contacts contacts = new Contacts();
    contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
    contacts.SearchAsync(displayName,FilterKind.DisplayName,null);
 
    //search for all contacts
    //contacts.SearchAsync(string.Empty, FilterKind.None, null);
}

Here is how we get the actual data using Results property of ContactsSearchEventArgs in the SearchCompleted handler. Have in mind that in some cases there are more than one e-mails,for example, so we will use FirstOrDefault() to get the first available.

NOTE: You will have to add a reference to System.Device in order to be able to get the physical address:

tip65-06

You will also have to include the following namespace:

using Microsoft.Phone.UserData;

 

 

 

 

private string displayName = "Andrew Hill";
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    foreach (var result in e.Results)
    {
        this.tbdisplayName.Text = "Name: " + result.DisplayName;
        this.tbEmail.Text = "E-mail address: " + result.EmailAddresses.FirstOrDefault().EmailAddress;
        this.tbPhone.Text = "Phone Number: " + result.PhoneNumbers.FirstOrDefault();
        this.tbPhysicalAddress.Text = "Address: " + result.Addresses.FirstOrDefault().PhysicalAddress.AddressLine1;
        this.tbWebsite.Text = "Website: " + result.Websites.FirstOrDefault();
    }
}

Example: To summarize, we can combine the both previous examples in a more complex one:

Step1: At first we select a Name from the Contacts application using AddressChooserTask

Step2: After that we will set displayName = e.DisplayName; in addressTask_Completed handler

Step3: Finally we will use SearchAsync() method (filtering by DisplayName) to search for more details.

完整的示例代码:

XAML:

<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Button Content="Launch Contacts App" Click="btnContacts_Click"/>
    <TextBlock x:Name="tbName" FontSize="25"/>
    <TextBlock x:Name="tbAddress" FontSize="25"/>
    <Button Content="Search for a Contact details" Click="btnSearch_Click"/>
    <TextBlock x:Name="tbdisplayName" FontSize="25"/>
    <TextBlock x:Name="tbEmail" FontSize="25"/>
    <TextBlock x:Name="tbPhone" FontSize="25"/>
    <TextBlock x:Name="tbPhysicalAddress" FontSize="25"/>
    <TextBlock x:Name="tbWebsite" FontSize="25"/>
</StackPanel>

C#:
AddressChooserTask addressTask;
private string displayName = "Andrew Hill";
// Constructor
public MainPage()
{
    InitializeComponent();
 
    addressTask = new AddressChooserTask();
    addressTask.Completed += new EventHandler<AddressResult>(addressTask_Completed);
}
 
private void btnContacts_Click(object sender, RoutedEventArgs e)
{
    addressTask.Show();
}
 
void addressTask_Completed(object sender, AddressResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        this.displayName = e.DisplayName;
        this.tbName.Text = "Name: " + e.DisplayName;
        this.tbAddress.Text = "Address: "+ e.Address;
    }
}
 
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
    Contacts contacts = new Contacts();
    contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
    contacts.SearchAsync(displayName,FilterKind.DisplayName,null);
 
    //search for all contacts
    //contacts.SearchAsync(string.Empty, FilterKind.None, null);
}
 
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    foreach (var result in e.Results)
    {
        this.tbdisplayName.Text = "Name: " + result.DisplayName;
        this.tbEmail.Text = "E-mail address: " + result.EmailAddresses.FirstOrDefault().EmailAddress;
        this.tbPhone.Text = "Phone Number: " + result.PhoneNumbers.FirstOrDefault();
        this.tbPhysicalAddress.Text = "Address: " + result.Addresses.FirstOrDefault().PhysicalAddress.AddressLine1;
        this.tbWebsite.Text = "Website: " + result.Websites.FirstOrDefault();
    }
}

tip65-04tip65-05

完整代码下载:

希望本文对你有所帮助。

加载中
0
Item
Item
不错是文章 顶一个
返回顶部
顶部