【问题标题】:get contact list with multiple phone numbers in windows phone 8 c#在windows phone 8 c#中获取具有多个电话号码的联系人列表
【发布时间】:2014-05-20 05:20:02
【问题描述】:

在我的 windows phone 应用程序中,我想获取 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>

下面是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";
            }
        }
    }
}

但是当我进入 CustomContact contact = new CustomContact(); 课程时,它会让我进入默认的构造函数,即空的。下面是我的 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] = "";
                }
            }
        }
    }
}

但它不能正常工作,也没有向我显示具有多个 PhoneNumber 的联系人列表,我在这一行遇到异常 contact.Number[i] = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString(); 并且异常是 Object reference not set to an instance of an object. 。我不明白我在哪里犯错。 请给我建议,等待回复。 谢谢。

【问题讨论】:

  • line c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString() in that count>0 但在某些时候 Element 为空,因此 ElementAt(i) 抛出异常

标签: c# windows-phone-8


【解决方案1】:

你必须再检查一个条件。

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

【讨论】:

  • 如果c.PhoneNumbers.ElementAt(i).PhoneNumber== null 怎么办?
  • @stackunderflow:它没有进入条件,这意味着PhoneNumber不为空。
  • 是的,但它不会让我将PhoneNumber 转换为contact.Number[i]。为什么?
  • 我想将PhoneNumber 加入contact.Number[i]
  • c.PhoneNumbers 中有电话号码吗?
【解决方案2】:

按照 Dhavel 说的做同样的事情,并检查这个

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

可能有些用户没有电话号码

【讨论】:

    【解决方案3】:

    希望对您有所帮助。 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;
        }
    }
    

    并在 GetContacts 后面的代码页面中将数字定义为字符串列表:

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

    。 . . 和 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";
            }
        }
    

    在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 2015-05-18
      相关资源
      最近更新 更多