通过电话号码搜索联系人,使用3位数字前缀进行过滤

时间:2015-04-23 07:22:17

标签: windows-phone-7 windows-phone-8 windows-phone windows-phone-8.1

我希望我的联系人中的所有电话号码都以特定的3位数开头,例如“012”,当我点击按钮时。

我一直在使用以下代码进行处理:

private void ButtonContacts_Click(object sender, RoutedEventArgs e)
{
    Contacts cons = new Contacts();

   //Identify the method that runs after the asynchronous search completes.
   cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

   //Start the asynchronous search.
   cons.SearchAsync("0109", FilterKind.PhoneNumber, "State String 5");
}


void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    try
    {
        //Bind the results to the user interface.
        ContactResultsData.DataContext = e.Results;
    }
    catch (System.Exception)
    {
        //No results
    }

    if (ContactResultsData.Items.Any())
    {
        ContactResultsLabel.Text = "results";
    }
    else
    {
        ContactResultsLabel.Text = "no results";
    }
}

FilterKind.PhoneNumber仅在至少与电话号码匹配的最后6位数时才有效 知道如何实现这个目标吗? 顺便说一下,我是初学者。

1 个答案:

答案 0 :(得分:1)

正如你所说,联系人api的过滤器只有在最后六位数相同的情况下才匹配,你可以在documentation中看到它,所以你不能使用它。

在我看来,最好的方法是接收所有联系人列表,然后使用LINQ找到你想要的联系人。

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    var contacts = new Contacts();
    contacts.SearchCompleted += Contacts_SearchCompleted;
    contacts.SearchAsync(null, FilterKind.None, null);
}

void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    var results = e.Results.ToArray();
    var myContacts = results.Where(c => c.PhoneNumbers.Any(p => p.PhoneNumber.StartsWith("66"))).ToArray();
}

您可以在最后一行查看查询以查找其中一些号码以66开头的联系人。您可以更改此查询,以便匹配所需的号码。