Windows phone 7 按电话号码搜索联系人,使用3位前缀进行筛选

Windows phone 7 按电话号码搜索联系人,使用3位前缀进行筛选,windows-phone-7,windows-phone-8,windows-phone,windows-phone-8.1,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

我想在我的联系人中获取所有以特定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";
    }
}
private void按钮联系\u点击(对象发送者,路由目标)
{
Contacts cons=新联系人();
//标识异步搜索完成后运行的方法。
cons.SearchCompleted+=新事件处理程序(联系人\u SearchCompleted);
//启动异步搜索。
cons.SearchAsync(“0109”,FilterKind.PhoneNumber,“状态字符串5”);
}
无效联系人搜索已完成(对象发件人、联系人搜索目标e)
{
尝试
{
//将结果绑定到用户界面。
ContactResultsData.DataContext=e.结果;
}
捕获(系统异常)
{
//没有结果
}
if(ContactResultsData.Items.Any())
{
ContactResultsLabel.Text=“结果”;
}
其他的
{
ContactResultsLabel.Text=“无结果”;
}
}
但是,
FilterKind.PhoneNumber
只有在至少与电话号码的最后6位匹配时才起作用。
你知道如何做到这一点吗?

顺便说一句,我完全是个初学者。

正如您所说,联系人api的过滤器只匹配最后六位数字相同的情况,您可以在中看到它,因此您无法使用它

在我看来,最好的方法是接收所有联系人列表,然后使用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开头。您可以根据需要更改此查询以匹配所需的数字

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();
}