0

编辑:新问题 - 为了清楚起见和新代码,请参阅在 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);
            }
        }
    }
}
4

2 回答 2

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);
            }
于 2013-08-22T16:15:04.677 回答
0

考虑为Group Contacts应用程序实现的这个示例:

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;
            }
}
于 2014-08-23T13:22:45.090 回答