在WindowsPhone8中生成基于MVVM Light的LongListSelector拼音检索绑定

目前正在用MVVM Light改造自己的APP,折腾了很久,终于克服了一个一个问题,下面对此作相应记录

如何给LongListSelector添加数据绑定?数据内容不添加在MainWindow.cs中。

这里,微软给了我们如何利用WindowsPhone8的LongListSelector作数据绑定,绑定效果非常优秀,但是其将数据直接添加进了MainPage.cs,现在我们基于MVVM Light将其作大改进。

(基础是安装MVVM Light tookit,创建新项目直接创建MVVM(WP8))

1.参照以上链接,创建AlphaKeyGroup类,主要作用在于将每个数据分发到不同的拼音类中。(!!注意,此处应该将AlphaKeyGroup类的访问属性改为public)

2.创建自己的Model,我在这里继续引用示例中的AddressBook类

    public class AddressBook
    {
        public string FirstName
        {
            get;
            set;
        }
        public string LastName
        {
            get;
            set;
        }
        public string Address
        {
            get;
            set;
        }
        public string Phone
        {
            get;
            set;
        }
        public AddressBook(string firstname, string lastname, string address, string phone)
        {
            this.FirstName = firstname;
            this.LastName = lastname;
            this.Address = address;
            this.Phone = phone;
        }
    }

 3.创建新类AddressBookCollection,添加相应数据,并添加至数据源DataSource

    public class AddressBookCollection
    {
        private readonly ObservableCollection<AddressBook> _books = new  ObservableCollection<AddressBook>();
        public ObservableCollection<AddressBook> Books
        {
            get { return _books; }
        }
        //  
        private List<AlphaKeyGroup<AddressBook>> _dataSource = new List<AlphaKeyGroup<AddressBook>>();

        public List<AlphaKeyGroup<AddressBook>> DataSource
        {
            get { return _dataSource; }
        }

        public AddressBookCollection()
        {
            _books.Add(new AddressBook("Joe", "Smith", "US", "48579347594"));
            _books.Add(new AddressBook("Jim", "Johnson", "UK", "3423423423"));
            _books.Add(new AddressBook("Mary", "Robert", "India", "9384394793"));
            _dataSource = AlphaKeyGroup<AddressBook>.CreateGroups(_books,
                System.Threading.Thread.CurrentThread.CurrentUICulture,
                (AddressBook s) => { return s.LastName; }, true);
        }
    }

 4.在MainViewModel中,添加对应属性,更新数据

        public const string CustomersPropertyName = "DataSource";
        public List<AlphaKeyGroup<AddressBook>> DataSource
        {
            get
            {
                var addcol = new AddressBookCollection();
                return addcol.DataSource;
            }
        }

5.在MainPage.xaml中,添加LongListSelector三个样式资源(在文章开始链接内找到,在页面最下方)

6.添加LongListSelector

<phone:LongListSelector
  x:Name="AddrBook"
  JumpListStyle="{StaticResource AddrBookJumpListStyle}"
  Background="Transparent"
  GroupHeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"
  ItemTemplate="{StaticResource AddrBookItemTemplate}"
  LayoutMode="List"
  IsGroupingEnabled="true"
  HideEmptyGroups ="true"
  ItemsSource="{Binding DataSource}"
/>

 运行示例代码,将得到以下页面:

希望大家不要再走我的弯路...

 

posted @ 2013-05-17 10:04  valentine is me  阅读(476)  评论(0编辑  收藏  举报