windows phone开发学习3:MVVM模式总结

MVVM组成 Model  View  View-Model

Model:数据访问层--Web Service Rest Services , Generic Collections

ViewUI层  ---控件,数据绑定,Commands

View-Model:是View的抽象,负责ViewModel之间的信息交换,将viewCommand传送到Model   -----属性,集合,Command

View 与 ViewModel之间连接可以通过下面的方式:

Binding Data:实现ViewView-Model之间数据的通信

Command:实现ViewView-Model之间操作命令的通信

AttachBehavior:实现控件加载过程中的初始化等操作

主要思想:

View绑定到ViewModel,然后执行一些命令在向它请求一个动作。而反过来,ViewModelModel通讯,告诉它更新来响应UI。这样便使得为应用构建UI非常的容易。往一个应用程序上贴一个界面越容易,外观设计师就越容易使用Blend来创建一个漂亮的界面。同时,当UI和功能越来越松耦合的时候,功能的可测试性就越来越强。

数据绑定:

数据源(Source):是数据绑定的数据提供者

路径(Path):数据源的具体属性的数值

元素名称(ElementName):数据源是UI控件的时候使用

目标(Target):数据源传送到哪里去,也就是数据源对应的绑定对象

关联(Binding):建立数据绑定,使数据源与目标关联起来

关联模式(Mode):数据绑定的模式(OneWayTwoWayOneTime)(OneWay是单方向的绑定,只是一个方向的数据改变;TwoWay是双方向的改变,彼此都能改变对方;OneTime只有一次改变,后面就失效了)

绑定的方式:

1、用元素值绑定:如<Text = {Binding ElementName = slider,Path=Value}>通过一个控件的Value改变来绑定这个另外一个控件的Text

2、绑定转换器:Binding... Converter={Statice ConverterKey};需要继承IValueConverter然后重写Convert方法和ConvertBack方法

3、绑定集合:常用到ListBox这类控件;ViewModel中继承INotifyPropertyChanged,然后定义一个ObservableCollection<T>的集合,将数据添加进去。

Command的实现

类似按钮的click事件,只是不再xaml后台代码实现了,通过command将此类事件在ViewModel层实现。

自定义的Command需要使用附件属性的方法来实现DependencyProperty

使用步骤:

1、View层:添加命名空间:xmlns:my=ViewModelxmlns:my_Interactivity=Commandxmlns:Custom=clr-namespace:System.Windows.Interactivity;assembly=System.Windows. Interactivityxmlns:ic=clr-namespace:Microsoft.Expression.Interactivity.Core:...

2、ViewModel层:定义ICommand属性如:public ICommand MinRadius{get ; private set;} 在注册单击Command事件如:MinRadius = new ActionCommand(p=>radius = 100);

3、Command层定义Command操作

a) 注册一个附加Command public static readonly DependencyProperty CommandNameProperty = DependencyProperty.Register(CommandName , typeof(object),typeof(ExecuteCommandAction) , null);

b) 注册一个附加Command参数 : public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(CommandParameter , typeof(object), typeof(ExecuteCommandAction),null);

c) 重载Invoke方法:protected override void Invoke(object parameter) { }

AttachedBehaviors的实现

View层:添加命名空间 local ; 设置属性localBehavior.Brush=gold

//注册一个附加属性BrushProperty,在XAML中名字为Brush,是Brush类型,在Hover类中,PropertyMetadata初始化元数据

        public static readonly DependencyProperty BrushProperty = DependencyProperty. RegisterAttached("Brush",typeof(Brush),typeof(Behavior),new  PropertyMetadata (nullnew PropertyChangedCallback(OnHoverBrushChanged)));

        /// 获取属性值

        public static Brush GetBrush(DependencyObject obj){

            return (Brush)obj.GetValue(BrushProperty);}

        /// 设置属性值

        public static void SetBrush(DependencyObject obj, Brush value){

            obj.SetValue(BrushProperty, value);}

        /// 属性初始化

        private static void OnHoverBrushChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args){

            //获取属性所在的TextBlock控件

            TextBlock control = obj as TextBlock;

            //注册控件的事件

            if (control != null){

                //注册鼠标进入事件

                control.MouseEnter += new MouseEventHandler(OnControlEnter);

                //注册鼠标离开事件

                control.MouseLeave += new MouseEventHandler(OnControlLeave);}}

        /// 鼠标进入事件

        static void OnControlEnter(object sender, MouseEventArgs e){ }

        /// 鼠标离开事件

        static void OnControlLeave(object sender, MouseEventArgs e){ 

 

 

原文地址:https://www.cnblogs.com/liutianwen/p/3014612.html