In my windows phone application, I am want to get contact list and I follow this link http://stackoverflow.com/questions/18387249/selecting-contacts-in-windows-phone-8
and working like below:
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 = "";
}
}
In the above code I am creating class class of CustomContact and below is the xaml.cs page
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>
But when I put listOfContacts
in listbox ContactResultsData
it not shows contacts in the list box and when I put breakpoint at this line ContactResultsData.DataContext = listOfContacts
the contacts are already exists in the listOfContacts
then why it not shows in the listbox ContactResultsData
. Kindly suggest me, waiting for your reply. Thanks
My suggestion is to expose listOfContacts
to public property. Also change its type to ObservableCollection
. Change XAML like this:
<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>
In your code-behind you should operate on contacts public property only by removing and adding items - NOT changing reference to it, because that'll break binding between XAML and code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.