如何在 WP8 中的<工具包:LongListMultiSelector 中绑定电话联系人

本文关键字:LongListMultiSelector 绑定 电话 联系人 工具包 WP8 中的 | 更新日期: 2023-09-27 18:33:22

我想在 LongListMultiSelector 中显示我的手机联系人。早些时候,我使用列表框来显示电话联系人...我的实现是这样的:XAML

 <ListBox Name="ContactResultsData" ItemsSource="{Binding}"  Height="331" Margin="12,0" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" >
                            <Border BorderThickness="2" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" >
                                <Image Source="{Binding Converter={StaticResource ContactPictureConverter}}" Width="48" Height="48" Stretch="Fill"  />
                            </Border>
                            <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Margin="18,8,0,0" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

和 C#

private void SearchContacts_Click(object sender, RoutedEventArgs e)
        {
           ContactResultsData.DataContext = null;
            Contacts cons = new Contacts();
            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
        }
        void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            try
            {
                ContactResultsData.DataContext = e.Results;
            }
            catch (System.Exception)
            {
            }
        }

现在尽管有列表框,我想在 LongListMultiSelector 中显示它。

我该怎么做?

现在我正在使用视图模型

public partial class ContactsView : PhoneApplicationPage
    {
public ContactsViewModel()
        {
            var cons = new Microsoft.Phone.UserData.Contacts();
            cons.SearchAsync(String.Empty, FilterKind.None, null);
            cons.SearchCompleted += ContactsSearchCompleted;
        }
        private void ContactsSearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            PhoneContacts = new ObservableCollection<Contact>(e.Results.OrderBy(c => c.DisplayName));
        }
        private ObservableCollection<Contact> _phoneContacts;
        public ObservableCollection<Contact> PhoneContacts
        {
            get { return _phoneContacts; }
            set
            {
                _phoneContacts = value;
                RaisePropertyChanged("PhoneContacts");
            }
        }
}

如何将此视图模型绑定到 LongListMultiSelector,以便 ObservableCollection 联系人数据可见?就像这个数字一样..我想显示的不是邮件详细信息,而是联系人详细信息。电话联系人 http://blogs.windows.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-53-84-metablogapi/clip_5F00_image010_5F00_41CD2003.png

如何在 WP8 中的<工具包:LongListMultiSelector 中绑定电话联系人

在大多数情况下,简单地用phone:LongListSelector替换ListBox的所有实例就可以了。在许多情况下,不建议使用 LongListSelector 而不是 ListBox(因为会出现一些意外的功能),但它应该可以正常工作。

<phone:LongListSelector ItemsSource="{Binding DataList}">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate/>
    </phone:LongListSelector.ItemTemmplate>
</phone:LongListSelector>

我修改了我的视图模型,如下所示:

public class ContactsViewModel : ViewModelBase
{
    public ContactsViewModel()
    {
        Contacts cons = new Contacts();
        cons.SearchAsync(String.Empty, FilterKind.None, null);
        cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
    }
    void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
    {
        try
        {
            PhoneContactsList = new List<Contact>(e.Results.OrderBy(c => c.DisplayName));
            System.Diagnostics.Debug.WriteLine("PhoneContacts phone" + PhoneContactsList);
        }
        catch (System.Exception)
        {
            //That's okay, no results
        }
    }
    //--------------------------------
    private List<Contact> _phoneContactsList;
    public List<Contact> PhoneContactsList
    {
        get { return _phoneContactsList; }
        set
        {
            _phoneContactsList = value;
            RaisePropertyChanged("PhoneContactsList");
        }
    }
}

而不是可观察集合,我使用的是列表。在 xaml 中,我将电话联系人列表绑定到我的长列表多选器,如下所示:

<toolkit:LongListMultiSelector x:Name="contactList" 
                                                Margin="0,14,-12,0"
                                                ItemsSource="{Binding PhoneContactsList}"
                                                LayoutMode="List"
                                                ItemTemplate="{StaticResource ContactItemTemplate}"
            />

其中联系人项目模板如下:

 <DataTemplate x:Key="ContactItemTemplate">
            <StackPanel Margin="0,-14,0,24" >
                <TextBlock Text="{Binding Path=DisplayName, Mode=OneWay}" 
                                       Margin="0,0,0,-4"
                                       FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
                                       FontFamily="{StaticResource PhoneFontFamilySemiLight}"/>
                <ListBox ItemsSource="{Binding Path=PhoneNumbers}" Height="60"  Margin="36,0,0,0">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{Binding Path=Kind, Mode=OneWay}" />
                                <TextBlock Grid.Column="1" Text=":  " />
                                <TextBlock Grid.Column="2" Text="{Binding Path=PhoneNumber, Mode=OneWay}" />
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
        </DataTemplate>