简体   繁体   English

在Windows Phone 8中获取包含多个电话号码的联系人列表

[英]get contact list with multiple phone numbers in windows phone 8 c#

In my windows phone application I want to get contact list of windows phone 8 and each contact have two or more phone numbers and I want to display contact name with phone numbers in my application and I am trying below: 在我的Windows手机应用程序中,我想获得Windows Phone 8的联系人列表,每个联系人都有两个或更多的电话号码,我想在我的应用程序中显示联系人姓名和电话号码,我在下面尝试:

xaml page:
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel>
                <TextBlock Name="ContactResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />

                <ListBox Name="ContactResultsData" ItemsSource="{Binding listOfContacts}" Height="200" Margin="24,0,0,0" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Name="ContactResultsName" Text="{Binding Path=Name}" />

                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
            <Button x:Name="ButtonContacts"
                    Content="Get All Contacts"
                    FontSize="15"
                    Width="200"
                    Height="70"
                    Background="AliceBlue"
                    Foreground="Blue"
                    HorizontalAlignment="Left"
                    Click="ButtonContacts_Click"></Button>
            <Button x:Name="MergeContacts"
                    Content="Merge Contacts"
                    FontSize="15"
                    Width="200"
                    Height="70"
                    Background="AliceBlue"
                    Foreground="Blue"
                    HorizontalAlignment="Right"
                    Click="MergeContacts_Click"></Button>
        </Grid>

And below is xaml.cs page: 以下是xaml.cs页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GetContacts.Resources;
using Microsoft.Phone.UserData;

namespace GetContacts
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }
        private void ButtonContacts_Click(object sender, RoutedEventArgs e)
        {
            Contacts cons = new Contacts();
            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

            cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
        }

        void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            try
            {
                List<CustomContact> listOfContact = new List<CustomContact>();
                foreach (var c in e.Results)
                {
                    CustomContact contact = new CustomContact();
                    contact.Name = c.DisplayName;
                    int count = c.PhoneNumbers.Count();
                    for (int i = 0; i < count; i++)
                    {
                        if (count > 0)
                        {
                            contact.Number[i] = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
                        }
                        else
                        {
                            contact.Number[i] = "";
                        }

                    }
                    listOfContact.Add(contact);
                }

                ContactResultsData.ItemsSource = listOfContact;
            }
            catch (System.Exception)
            {
                //No results
            }
            if (ContactResultsData.Items.Any())
            {
                ContactResultsLabel.Text = "results";
            }
            else
            {
                ContactResultsLabel.Text = "no results";
            }
        }
    }
}

but when I am going into the class CustomContact contact = new CustomContact(); 但是当我进入CustomContact contact = new CustomContact();类时, CustomContact contact = new CustomContact(); its going me into the default contructor ie empty. 它让我进入默认的构造函数即空。 And below is my CustomContact class 以下是我的CustomContact类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GetContacts.Resources;
using Microsoft.Phone.UserData;

namespace GetContacts
{
    class CustomContact
    {
        public string Name { get; set; }
        public string[] Number
        {
            get;
            set;
        }
       // public string Number1 { get; set; }

        public CustomContact()
        {
        }

        //CTOR that takes in a Contact object and extract the two fields we need (can add more fields)
        public CustomContact( Contact contact)
        {
            Name = contact.DisplayName;
            int count = contact.PhoneNumbers.Count();
            for (int i = 0; i < count; i++)
            {
                if (count > 0 && contact.PhoneNumbers.ElementAt(i).PhoneNumber != null && !string.IsNullOrEmpty(contact.PhoneNumbers.ElementAt(i).PhoneNumber))
                {
                    Number[i] = contact.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
                }
                else
                {
                   Number[i] = "";
                }
            }
        }
    }
}

But its not working fine and not show me the list of contacts with multiple PhoneNumber and I am getting exception at this line contact.Number[i] = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString(); 但它没有正常工作,并没有显示多个PhoneNumber的联系人列表,我在这行contact.Number[i] = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString(); and exception is Object reference not set to an instance of an object. 异常是Object reference not set to an instance of an object.Object reference not set to an instance of an object. . I don't understand where I make mistake. 我不明白我哪里弄错了。 Kindly suggest me, waiting for reply. 请建议我,等待回复。 Thanks. 谢谢。

you have to check the one more condition. 你必须再检查一个条件。

if(count>0 && c.PhoneNumbers.ElementAt(i)!=null && !string.IsNullOrEmpty(c.PhoneNumbers.ElementAt(i).PhoneNumber))
{
contact.Number[i] = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
}

Do the same as Dhavel said and also check for this 和达维尔说的一样,也检查一下

c.PhoneNumbers.ElementAt(i).PhoneNumber== null ?

There might be some users who didn't have phone numbers 可能有一些用户没有电话号码

I hope this help you. 我希望这对你有帮助。 in the CustomContact model Numbers Variable should be list of strings for multiple numbers of a contact, so CustomContact model should be: 在CustomContact模型中,Numbers变量应该是多个联系人号码的字符串列表,因此CustomContact模型应该是:

 class CustomContact
{
    public string Name { get; set; }

    public List<string> Numbers { get; set; }


    public CustomContact()
    {
    }

    public CustomContact(string displayName, List<string> phoneNumbers)
    {
        this.Name = displayName;
        this.Numbers = phoneNumbers;
    }
}

and in GetContacts behindcode page define numbers as string list: 并在GetContacts后面的代码页中将数字定义为字符串列表:

 public partial class GetContacts : PhoneApplicationPage
{
    List<string> numbers = new List<string>();
    public GetContacts()
    {
        InitializeComponent();
    }

. . . and Contacts_SearchCompleted like below: 和Contacts_SearchCompleted如下:

 void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
    {

            List<CustomContact> listOfContact = new List<CustomContact>();
            foreach (var c in e.Results)
            {

                CustomContact contact = new CustomContact();
                contact.Name = c.DisplayName;
                 numbers.Clear();
                int count = c.PhoneNumbers.Count();
                for (int i = 0; i < count; i++)
                {

                    if (count > 0)
                    {

                    numbers.Add (c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString());
                    }
                    contact.Numbers = numbers;

                }
                listOfContact.Add(contact);

            }

            ContactResultsData.ItemsSource = listOfContact;

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

and in UI to show name and numbers use this code 并在UI中显示名称和数字使用此代码

  <ListBox Name="ContactResultsData" ItemsSource="{Binding listOfContacts}" Height="200">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Name="ContactResultsName" Text="{Binding Path=Name}" />
                        <ListBox Name="ContactResultsNumbers" ItemsSource="{Binding Numbers}" Foreground="White" Height="50" Margin="24,0,0,0" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM