检索姓名列表 c# wp8 的联系电话
本文关键字:wp8 联系电话 列表 检索 | 更新日期: 2023-09-27 18:28:35
编辑:新问题 - 为了清楚和新代码,请参阅在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);
}
}
}
}
使用 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);
}
请考虑为"组联系人"应用实现的此示例:
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;
}
}