编辑:新问题 - 为了清晰起见&新代码见Selecting contacts in windows phone 8
无论是谁投票,都非常感谢。很有建设性。
由于这里的MSDN和支持,我得到了以下代码,它填充了一个联系人列表,允许用户在将它们保存到List对象之前选择多个联系人。
但是,我现在想知道如何检索与检索到的每个名称相关联的联系号码。我已经尝试了后续搜索,但我觉得我做错了,每次只是破坏了代码。
非常感谢任何帮助,谢谢。
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);
}
}
}
}
答案 0 :(得分:2)
使用var而不是object
我理解你的要求,但我不理解你的代码,无论如何
我只给出一个密钥,就像ContactResultsData.SelectedItems
是你的数据库值一样,然后尝试这种方式
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);
}
答案 1 :(得分:0)
考虑一下为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}}" />
。
代码:
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;
}
}