Silverlight中如何使用MVVM架构

来源:互联网 发布:夜光玩具 辐射 知乎 编辑:程序博客网 时间:2024/05/01 18:00

MVVM架构: Model + View + ViewModel

Model层是数据类的提供者;View是表示层;ViewModel是逻辑层。

该实例非常简单,只是把数据体加载到页面DataGrid上,没有ICommand相关的命令方法。

首先构筑Model层,这里新建User类及User类的集合UserModel类:

由于在View界面上需要Binding到User的属性,所以这里需要实现INotifyPropertyChanged接口,来提供通知功能。

如果不使用InotifyPropertyChanged声明,也可以在xaml页面上进行绑定,只是当后台数据改变时,不能通知xaml页面数据发生改变。而且当xaml页面数据发生改变的时候,也不能通知后台数据发生改变。

using System;using System.Collections.Generic;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;using System.ComponentModel;namespace MVVMDemo1{public class User:INotifyPropertyChanged{public User(){// Insert code required on object creation below this point.}        //定义属性,并且使用NotifyPropertyChanged方法进行绑定通知        //如果不适用InotifyPropertyChanged声明,也可以在xaml页面上进行绑定,        //只是当后台数据改变时,不能通知xaml页面数据发生改变。        //而且当xaml页面数据发生改变的时候,也不能通知后台数据发生改变。        private string _Name;        public string Name        {            get { return _Name; }            set            {                _Name = value;                NotifyPropertyChanged("Name");            }        }        private bool _Active;        public bool Active        {            get { return _Active; }            set            {                _Active = value;                NotifyPropertyChanged("Active");            }        }        private string _Birthday;        public string Birthday        {            get { return _Birthday; }            set            {                _Birthday = value;                NotifyPropertyChanged("Birthday");            }        }        //实现INotifyPropertyChanged接口的方法        private void NotifyPropertyChanged(string info)        {            if (PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs(info));            }        }        public event PropertyChangedEventHandler PropertyChanged;    }}


UserModel类:该类是User类的集合类,这里使用了ObservableCollection<T>泛型类,该类可以自动提供INotifyPropertyChanged方法,不要在属性里编写该方法,也能自动提供通知。

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Collections.ObjectModel;namespace MVVMDemo1.BusinessObject{    public class UserModel    {        public UserModel()        { }        //ObservableCollection<T> 类        //表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。         //ObservableCollection<T> 类,它是实现 INotifyCollectionChanged 接口的数据集合的内置实现。        public static ObservableCollection<User> GetUserList()        {            ObservableCollection<User> _Users = new ObservableCollection<User>();            _Users.Add(new User() { Name = "Bill Moore", Birthday = "06/22/1978", Active = true });            _Users.Add(new User() { Name = "Victor Gad", Birthday = "10/22/1999", Active = true });            _Users.Add(new User() { Name = "Paul Gebo", Birthday = "08/15/1935", Active = false });            _Users.Add(new User() { Name = "Thomas Train", Birthday = "05/19/1967", Active = true });            return _Users;        }    }}


接下来构筑ViewModel层:该层是为了把View层上的数据进行Binding使用,如果xaml页面上需要使用n个binding属性,那么ViewModel层也需要声明N个相对应的属性来提供Binding使用。

using System;using System.ComponentModel;using System.Collections.ObjectModel;using MVVMDemo1.BusinessObject;namespace MVVMDemo1{    //ViewModel一定要实现INotifyPropertyChanged接口public class MainViewModel : INotifyPropertyChanged{        public MainViewModel()        {            Users = UserModel.GetUserList();        }        private ObservableCollection<User> _Users = new ObservableCollection<User>();        public ObservableCollection<User> Users        {            get { return _Users; }            set             {                //由于ObservableCollection<T> 类,它是实现 INotifyCollectionChanged 接口的数据集合的内置实现。                //所以这里就不用再写: NotifyPropertyChanged("Users");                _Users = value;            }        }        //INotifyPropertyChanged.PropertyChanged事件是在更改属性值的时候发生。        //PropertyChanged事件可以通过将null或者String.Empty用作PropertyChangedEventArgs中的属性名,        //指示该对象的所有属性都已变更。        private void INotifyPropertyChanged(string p)        {            if (PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs(p));            }        }        //实现 INotifyPropertyChanged 接口事件        public event PropertyChangedEventHandler PropertyChanged;    }}


最后是构筑View层,这里的View层非常简单,只是一个DataGrid来加载User数据而已,注意这里的Binding,以及需要制定DataContext为哪个ViewModel,这样才能通过DataContext把当前xaml文件里的Binding绑定到哪个ViewModel上的属性。

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:MVVMDemo1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ec="http://schemas.microsoft.com/expression/2010/controls" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:Ignorable="d" x:Class="MVVMDemo1.MainPage"Width="640" Height="480">    <StackPanel x:Name="LayoutRoot" Background="White">        <!--这里把ViewModel数据绑定到DataGrid上,ItemsSource后的Binding的意思是指绑定到ViewModel上的Users属性,        而且这个属性必须是用INotifyPropertyChanged声明过的才可以.        这里还需要主要必须要有DataContext数据上下文才可以完成绑定,可以再后置代码中声明,也可以再xaml文件里声明-->        <sdk:DataGrid Margin="63,33,48,38" ItemsSource="{Binding Users}" AutoGenerateColumns="False">            <sdk:DataGrid.Columns>                <!--这里的Bindding不用写成Users.Name,因为在ItemsSource中已经指定了Source为Users,所以可直接写属性Name-->                                <sdk:DataGridTextColumn Header="姓名" Binding="{Binding Name}"/>                <sdk:DataGridCheckBoxColumn Header="激活与否" Binding="{Binding Active}"/>                <sdk:DataGridTextColumn Header="生日" Binding="{Binding Birthday}"/>            </sdk:DataGrid.Columns>        </sdk:DataGrid>                    </StackPanel></UserControl>

后台文件制定DataContext:

using System;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace MVVMDemo1{public partial class MainPage : UserControl{public MainPage(){// Required to initialize variablesInitializeComponent();            //在这里指定DataContext数据源为ViewModel            this.DataContext = new MainViewModel();}}}


源代码: http://download.csdn.net/detail/eric_k1m/5838685

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 婴儿大便稀水样怎么办 小孩晚上睡不着觉怎么办 小孩小鸡头红怎么办 儿童睡觉磨牙齿怎么办 幼儿小鸡红肿疼怎么办 宝宝小鸡淹了怎么办 小孩的小鸡肿怎么办 小鸡脚趾歪了怎么办 小鸡脖子歪了怎么办 宝宝的小鸡红怎么办 游戏联不了网络怎么办 光敏印章进水了怎么办 照片打印出来黑怎么办 企业股东签名不符怎么办 电脑签字签不了怎么办 刘海的碎发怎么办 车牌号全是单数怎么办 品正通保车险超市怎么办营业执照 车辆咨询服务门市怎么办营业执照 被互盾科技骗了怎么办 家具生意不好怎么办啊 今年家具店生意不好怎么办 奶茶店位置不好怎么办 早餐店位置不好怎么办 木工做的不好怎么办 淘宝昵称改不了怎么办 淘宝店关门售后怎么办 店里生意不好怎么办?解决方案 淘宝店铺没有生意怎么办 淘宝店做大了应该怎么办 汽车维修没生意怎么办 淘宝买家具安装怎么办 投标时未记主材费结算时怎么办 不敢买自慰棒怎么办 车被扎了个洞怎么办 企业欠税交不起怎么办 组织代码查不到怎么办 u盾电量不足怎么办 对公账户拍照怎么办 个人怎么办对公账户 车辆超过年检日期怎么办