如何使用Windows Phone 8的电话号码选择器任务获取电子邮件,显示姓名,号码

时间:2014-10-27 09:31:33

标签: c# windows-phone-8

我正在尝试收到电子邮件,显示姓名和手机号码形式的电话号码选择器任务,但它只提供s显示名称和电话号码作为回报我也希望电子邮件也可以返回。它可以通过Windows Phone 8的电子邮件地址选择器任务完成,但不会提供电话号码,以便如何实现它。在一个事件中获取所有3个细节

email address chooser code

phone number chooser task code

1 个答案:

答案 0 :(得分:1)

如果您需要所有信息,可能必须在给定其中一个选择器的数据输出的情况下搜索联系人列表,并获取匹配的条目。

让我们举一个PhoneNumberChooserTask

的例子
using System.Linq;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.UserData;

// create the phone number chooser
PhoneNumberChooserTask phoneNumberChooserTask;
phoneNumberChooserTask = new PhoneNumberChooserTask();
phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
phoneNumberChooserTask.Show();

// user has chose a contact from the list
void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        // At this point, we know the Phone Number and Display Name only
        // so lets search for all Contacts that have the same Phone Number and Display Name

        // create the search, we are going to filter by Display Name and past the Phone Number as the third variable (state)            
        Contacts cons = new Contacts();
        cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
        cons.SearchAsync(e.DisplayName, FilterKind.DisplayName, e.PhoneNumber);
    }
}

// search is complete
// lets use some LINQ and select out the matching data we want (magic.. I know)
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    Contact fullcontact = null;

    // query
    var q = from contact in e.Results
            from pn in contact.PhoneNumbers
            where pn.PhoneNumber.Equals((string)e.State, StringComparison.InvariantCultureIgnoreCase)
            select contact;

    // loop through all the matches (should be 1, if any)
    foreach (Contact c in q)
    {
       // save the contact
       fullcontact = c;   
    }                 


    // at this point fullcontact should contain everything if not null
    // loop through the phone numbers/emails (usually they will only contain 1
    // (unless, you really like to keep your contacts upto date)
    if(fullcontact != null)
    {
        MessageBox.Show(fullcontact.DisplayName);

        // loop phone numbers
        foreach(ContactPhoneNumber cpn in fullcontact.PhoneNumbers)
        {
            MessageBox.Show(cpn.PhoneNumber);
        }

        // loop emails
        foreach(ContactEmailAddress cea in fullcontact.EmailAddresses)
        {
            MessageBox.Show(cea.EmailAddress);
        }
    }
}