2014-05-20 43 views
0

在我的windows phone應用程序中,我想要獲取windows phone 8的聯繫人列表,並且每個聯繫人都有兩個或多個電話號碼,並且我希望顯示聯繫人姓名和電話號碼我的應用程序,我想下面:在windows phone 8中獲取多個電話號碼的聯繫人列表c#

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] = ""; 
       } 
      } 
     } 
    } 
} 

但它不是工作的罰款,而不是告訴我多******中國的聯繫人列表,我在這行contact.Number[i] = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();越來越例外,例外的是Object reference not set to an instance of an object.。我不明白我犯了什麼錯誤。 請提出我,等待回覆。 謝謝。

+0

線c.PhoneNumbers.ElementAt(ⅰ).PhoneNumber.ToString()但是在某些時候Element是空的,所以ElementAt(i)拋出一個異常 –

回答

2

你必須檢查一個更多的條件。

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

是的,我確實還是會遇到異常。 – user88

+0

如果'c.PhoneNumbers.ElementAt(i).PhoneNumber == null'會怎麼樣? – stackunderflow

+0

@stackunderflow:它沒有進入狀態,這意味着'PhoneNumber'不是空的。 – user88

0

一樣做Dhavel說還要檢查這個

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

有可能是誰沒有電話號碼

0

我希望這有助於你一些用戶。 在CustomContact型號的變量應該是一個聯繫人的多個號碼的字符串列表,所以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 behindcode頁面定義數字作爲字符串列表:

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顯示的名稱和序號的使用在該計數> 0這個代碼

<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> 
相關問題