2

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

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

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

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

这是相关的代码片段和随附的 XAML。非常感谢您的参与。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

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

1 回答 1

3

你可以创建你的班级

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 = "";
    }
}

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

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

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我们想要的对象。

private void ContactResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Contact contact = ContactResultsData.SelectedItem as Contact;
    if (contact != null)
    {
        CustomContact customContact = new CustomContact(contact);
    }
}
于 2013-08-22T17:52:07.130 回答