在我的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>
以下是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.
。我不明白我哪里弄错了。
请建议我,等待回复。
感谢。
答案 0 :(得分: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();
}
答案 1 :(得分:0)
和达维尔说的一样,并检查这个
c.PhoneNumbers.ElementAt(i).PhoneNumber== null ?
可能有些用户没有电话号码
答案 2 :(得分:0)
我希望这对你有所帮助。 在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>