如何绑定电话联系人

时间:2013-09-29 10:27:51

标签: c# xaml windows-phone-8

我想在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)
            {
            }
        }

现在,尽管有List Box,我想在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");
            }
        }
}

如何将此viewModel绑定到LongListMultiSelector,以便ObservableCollection联系人数据可见? 就像图中那样..而不是邮件细节,我想显示联系方式。 Phone Contacts http://blogs.windows.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-53-84-metablogapi/clip_5F00_image010_5F00_41CD2003.png

2 个答案:

答案 0 :(得分:0)

在大多数情况下,只需用手机替换ListBox的所有实例:LongListSelector工作得很好。有许多情况下不推荐使用LongListSelector而不是ListBox(因为会出现一些意外的功能),但它应该可以正常工作。

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

答案 1 :(得分:0)

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

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

而不是observablecollection,我使用的是列表。 在xaml中,我将PhoneContactsList绑定到我的longlistmultiselector,如下所示:

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

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