【发布时间】: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