[英]How to save a contact with multiple phone numbers on Windows Phone 8.1?
[英]get contact list with multiple phone numbers in windows phone 8 c#
在我的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 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.
為Object reference not set to an instance of an object.
。 我不明白我哪里弄錯了。 請建議我,等待回復。 謝謝。
你必須再檢查一個條件。
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 ?
可能有一些用戶沒有電話號碼
我希望這對你有幫助。 在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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.