在我的Windows手机应用程序中,我想获取联系人列表,并点击此链接http://stackoverflow.com/questions/18387249/selecting-contacts-in-windows-phone-8
并且工作如下:
public class CustomContact
{
public string Name { get; set; }
public string Number { get; set; }
public CustomContact()
{
}
public CustomContact(Contact contact)
{
Name = contact.DisplayName;
var number = contact.PhoneNumbers.FirstOrDefault();
if(number != null)
Number = number.PhoneNumber;
else
Number = "";
}
}
在上面的代码中我创建了CustomContact的类类,下面是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();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
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)
{
// MessageBox.Show(e.Results.Count().ToString());
try
{
List<CustomContact> listOfContacts = new List<CustomContact>();
foreach (var c in e.Results)
{
CustomContact contact = new CustomContact();
contact.DisplayName = c.DisplayName;
var number = c.PhoneNumbers.FirstOrDefault(); //change this to whatever number you want
if (number != null)
contact.Number = number.PhoneNumber;
else
contact.Number = "";
}
catch (System.Exception)
{
//No results
}
if (ContactResultsData.Items.Any())
{
ContactResultsLabel.Text = "results";
}
else
{
ContactResultsLabel.Text = "no results";
}
listOfContacts.Add(contact);
}
ContactResultsData.DataContext = listOfContacts;
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}" Height="200" Margin="24,0,0,0" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
</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>
</Grid>
但是,当我将listOfContacts
放入列表框ContactResultsData
时,它不会在列表框中显示联系人,当我在此行ContactResultsData.DataContext = listOfContacts
放置断点时,联系人已存在于{{1}那么为什么它不会显示在列表框listOfContacts
中。
请建议我,等待你的回复。
感谢
答案 0 :(得分:0)
我的建议是将listOfContacts
公开给公共财产。同时将其类型更改为ObservableCollection
。
像这样改变XAML:
<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 ContactList}" Height="200" Margin="24,0,0,0" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
</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>
</Grid>
在您的代码隐藏中,您应该仅通过删除和添加项来操作联系人公共属性 - 不更改对它的引用,因为这将破坏XAML和代码之间的绑定。