Reputation: 1081
EDIT: New question - for clarity & new code see Selecting contacts in windows phone 8
Whoever downvoted, thanks a lot. Very constructive.
I have, thanks to MSDN and support on here, got the following code to work, which populates a list of contacts and allows the user to select multiple contacts before saving them to a List object.
However, I would now like to know how to retrieve the contact numbers associated with each name retrieved. I have tried subsequent searches but I feel i have done this wrong and each time it just broke the code.
Any help would be greatly appreciated, thank you.
private void showContacts(object sender, RoutedEventArgs e)
{
Contacts cons = new Contacts();
//Identify the method that runs after the asynchronous search completes.
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
//Start the asynchronous search.
cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
}
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
//Do something with the results.
MessageBox.Show(e.Results.Count().ToString());
try
{
//Bind the results to the user interface.
ContactResultsData.DataContext = e.Results;
}
catch (System.Exception)
{
//No results
}
if (ContactResultsData.Items.Any())
{
ContactResultsLabel.Text = "results";
}
else
{
ContactResultsLabel.Text = "no results";
}
}
public void saveContacts(object sender, RoutedEventArgs e)
{
String strItem;
List<string> listOfNames = new List<string>();
foreach (Object selecteditem in ContactResultsData.SelectedItems)
{
//MessageBox.Show(selecteditem.ToString());
strItem = selecteditem as String;
ContactResultsLabel.Text = strItem;
listOfNames.Add(strItem);
//System.Diagnostics.Debug.WriteLine(strItem);
//MessageBox.Show("Saving " + strItem);
}
}
}
}
Upvotes: 0
Views: 856
Reputation: 11595
Consider this example that's implemented for Group Contacts app:
XAML:
<HyperlinkButton x:Name="CompanyButton" Content="{Binding ElementName=grid, Path=DataContext.SelectedContact.JobInfo, Converter={StaticResource WorkToTextConverter}}" Visibility="{Binding ElementName=grid, Path=DataContext.SelectedContact.JobInfo, Converter={StaticResource WorkToVisibilityConverter}}" IsEnabled="{Binding ElementName=grid, Path=DataContext.SelectedContact.Phones, Converter={StaticResource WorkPhoneExistsConverter}}"/>
<HyperlinkButton x:Name="MobileButton" Content="Mobile" Visibility="{Binding ElementName=grid, Path=DataContext.SelectedContact.Phones, Converter={StaticResource MobileToVisibilityConverter}}"/>
<HyperlinkButton x:Name="MessageButton" Content="Message" Visibility="{Binding ElementName=grid, Path=DataContext.SelectedContact.Phones, Converter={StaticResource MobileToVisibilityConverter}}"/>
<HyperlinkButton x:Name="EmailButton" Content="Email" Visibility="{Binding ElementName=grid, Path=DataContext.SelectedContact.Emails, Converter={StaticResource EmailsToVisibilityConverter}}" />
. .
Code:
private void Call(object e)
{
ContactPhone phone = null;
var kind = e as string;
switch (kind)
{
case "Mobile":
{
phone = SelectedContact.Phones.Where(p => p.Kind == ContactPhoneKind.Mobile).FirstOrDefault();
break;
}
case "Work":
{
phone = SelectedContact.Phones.Where(p => p.Kind == ContactPhoneKind.Work).FirstOrDefault();
break;
}
}
Upvotes: 0
Reputation: 38683
Use var instead of object
I understand your requirement , but i don't understand your code , anyway
I give just for a key like if the ContactResultsData.SelectedItems
has been your database values,then try this way
foreach (var selecteditem in ContactResultsData.SelectedItems)
{
//MessageBox.Show(selecteditem.ToString());
ContactResultsLabel.Text = selecteditem.Name;//The Name is ContactResultsData.SelectedItems return Column Field name
ContactNumberResult.Text = selecteditem.Number;//The Name is ContactResultsData.SelectedItems return Column Field name
listOfNames.Add(strItem);
//System.Diagnostics.Debug.WriteLine(strItem);
//MessageBox.Show("Saving " + strItem);
}
Upvotes: 2