在 Windows Phone 8 中选择联系人 [英] Selecting contacts in windows phone 8

查看:23
本文介绍了在 Windows Phone 8 中选择联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将基本联系人列表添加到我的应用中.

I am trying to add a basic contact list into my app.

到目前为止,该应用查询联系人存储并在列表中显示所有内容.

So far, the app queries the contact store and displays all in a list.

我需要的是一个包含用户从列表中选择的每个联系人的姓名和号码的数据结构.

What I require is a data structure containing the name and number of each contact that the user has selected from the list.

我很想看看你的想法.我相信这将是我错过的一些简单的事情,但我已经尝试了很多我现在非常困惑.

I would love to see your ideas. I am sure it will be something simple I have missed, but I have tried so much I am now very much confused.

这是相关的代码片段和随附的 XAML.非常感谢您的参与.C# 更新

Here is the relevant code snippet and accompanying XAML. Thank you so much for your time. C# UPDATED

namespace appNamespace
{
    public partial class contact : PhoneApplicationPage
    {
        public class CustomContact
        {
            public string Name { get; set; }
            public string Number { 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;
                var number = contact.PhoneNumbers.FirstOrDefault();
                if (number != null)
                    Number = number.PhoneNumber;
                else
                    Number = "";
            }
        }

        public contact()
        {
            InitializeComponent();
        }

        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)
        {
            List<CustomContact> listOfContacts = new List<CustomContact>();

            listOfContacts = e.Results.Select(x => new CustomContact()
            {
                Number = x.PhoneNumbers.FirstOrDefault() != null ? x.PhoneNumbers.FirstOrDefault().PhoneNumber : "",
                Name = x.DisplayName
            }).ToList();
        }

        private void ContactResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Contact contact = ContactResultsData.SelectedItem as Contact;
            if (contact != null)
            {
                CustomContact customContact = new CustomContact(contact);
            }
        }

    }
}

XAML

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,10" >

                <TextBlock Name="ContactResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />

                <ListBox Name="ContactResultsData" ItemsSource="{Binding}" Height="436" Margin="12,0" SelectionMode="Multiple" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Name="ContactResults" FontSize="{StaticResource PhoneFontSizeMedium}" Text="{Binding Path=DisplayName, Mode=OneWay}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
            <Button x:Name="showButton" Content="Show Contacts" HorizontalAlignment="Left" VerticalAlignment="Top" Width="218" Height="90" Margin="0,531,0,0" Click="showContacts"/>
            <Button x:Name="saveButton" Content="Save Contacts" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="238,531,0,0" Width="218" Height="90" Click="saveContacts"/>
        </Grid>

推荐答案

您可以创建您的班级

public class CustomContact
{
   public string Name { get; set; }
   public string Number { 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)
    {
        DisplayName = contact.DisplayName;
        var number = contact.PhoneNumbers.FirstOrDefault();
        if(number != null)
            Number = number.PhoneNumber;
        else
            Number = "";
    }
}

然后遍历结果并将它们添加到您的类中

And then iterate through the results and add them to your class

List<CustomContact> listOfContacts = new List<CustomContact>();
foreach (var c in e.Results)
{
    CustomContact contact  = new CustomContact();
    contact.DisplayName = c.DisplayName;
    var number = c.PhoneNumbers.FirstOrDefault(); //change this to whatever number you want
    if (number != null)
        contact.Number = number.PhoneNumber;
    else
        contact.Number = "";

    listOfContacts.Add(contact);
}
ContactResultsData.DataContext = listOfContacts;

您可以将上面的 foreach 循环缩短为单个 LINQ 查询

You could shorten the foreach loop above into a single LINQ query

listOfContacts = e.Results.Select(x => new CustomContact() 
                                  { 
                                     Number = x.PhoneNumbers.FirstOrDefault() != null ? x.PhoneNumbers.FirstOrDefault().PhoneNumber : "", 
                                     DisplayName = x.DisplayName 
                                  }).ToList();

根据评论更新.

假设您不使用上述方法,ListBox 将填充 Contact 对象(而不是我们的 CustomContact 对象).因此,我们将所选项目转换为 Contact 对象,并使用接收 Contact 对象的重载构造函数来创建我们想要的 CustomContact 对象.

Assuming you don't use the above method, the ListBox will be filled with Contact objects (and not our CustomContact objects). We therefore convert the selected item into a Contact object and use the overloaded constructor that takes in a Contact object to create the CustomContact object that we want.

private void ContactResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Contact contact = ContactResultsData.SelectedItem as Contact;
    if (contact != null)
    {
        CustomContact customContact = new CustomContact(contact);
    }
}

这篇关于在 Windows Phone 8 中选择联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆