在我的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.
。我不明白我犯了什麼錯誤。 請提出我,等待回覆。 謝謝。
線c.PhoneNumbers.ElementAt(ⅰ).PhoneNumber.ToString()但是在某些時候Element是空的,所以ElementAt(i)拋出一個異常 –