简体   繁体   English

在Windows Phone 8中选择联系人

[英]Selecting contacts in 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. 我相信这将是我想念的简单事情,但是我已经做了很多尝试,但现在却感到非常困惑。

Here is the relevant code snippet and accompanying XAML. 这是相关的代码片段和随附的XAML。 Thank you so much for your time. 非常感谢您的参与。 C# UPDATED C#更新

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 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>

You can create your class 您可以创建自己的课程

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;

You could shorten the foreach loop above into a single LINQ query 您可以将上面的foreach循环缩短为一个LINQ查询

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

Update based on comment. 根据评论更新。

Assuming you don't use the above method, the ListBox will be filled with Contact objects (and not our CustomContact objects). 假设您不使用上述方法,则ListBox中将填充Contact对象(而不是我们的CustomContact对象)。 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. 因此,我们将所选项目转换为Contact对象,并使用接受了Contact对象的重载构造函数来创建所需的CustomContact对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM