Reputation: 1164
In my windows phone application I want to get contact list of windows phone 8 and each contact have two or more phone numbers and I want to display contact name with phone numbers in my application and I am trying below:
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>
And below is 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();
}
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";
}
}
}
}
but when I am going into the class CustomContact contact = new CustomContact();
its going me into the default contructor i.e empty. And below is my CustomContact class
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] = "";
}
}
}
}
}
But its not working fine and not show me the list of contacts with multiple PhoneNumber and I am getting exception at this line contact.Number[i] = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
and exception is Object reference not set to an instance of an object.
. I don't understand where I make mistake.
Kindly suggest me, waiting for reply.
Thanks.
Upvotes: 0
Views: 1053
Reputation: 1
I hope this help you. in the CustomContact model Numbers Variable should be list of strings for multiple numbers of a contact, so CustomContact model should be:
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;
}
}
and in GetContacts behindcode page define numbers as string list:
public partial class GetContacts : PhoneApplicationPage
{
List<string> numbers = new List<string>();
public GetContacts()
{
InitializeComponent();
}
. . . and Contacts_SearchCompleted like below:
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";
}
}
and in UI to show name and numbers use this code
<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>
Upvotes: 0
Reputation: 7591
you have to check the one more condition.
if(count>0 && c.PhoneNumbers.ElementAt(i)!=null && !string.IsNullOrEmpty(c.PhoneNumbers.ElementAt(i).PhoneNumber))
{
contact.Number[i] = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
}
Upvotes: 2
Reputation: 3873
Do the same as Dhavel said and also check for this
c.PhoneNumbers.ElementAt(i).PhoneNumber== null ?
There might be some users who didn't have phone numbers
Upvotes: 0