zoukankan      html  css  js  c++  java
  • 项目经验之:MVVM初学者图形化笔记整理。。。

    这个模式,一下子把我的思路给打开。。让我眼前一亮。。居然可以这样将界面分离得如此彻底。。。。。。。。。。。

    大家一起学习。。。

    说说我的感受吧,一个小实例讲述了 MVVM实现原理:

    一个简单的例,将两个数相加。。

    MVVM,可能有人在说 M 不就是Model ,那个这Model是否一定要存在呢?

    但很多时候,这个Model可以不需要,,只用ViewModel就可以的。。还是结合代码说吧,

    这个就是ViewModel 对外公布的属性,Input1,Input2,Result ,看到吗,继承NotificationObject类,,,这个是数据属性。。如上图说明。。数据属性是双向的,那他又是如何来的呢  这个就是通知,

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MVM.ViewModels
    {
        /// <summary>
        /// 具有通知能力的就是viewmodel的基类 wpf中的DataBinding
        /// </summary>
        public class NotificationObject:INotifyPropertyChanged
        {
    
            #region INotifyPropertyChanged 成员
    
            public event PropertyChangedEventHandler PropertyChanged; 
    
            public void RaisePropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    //触发一个事件 PropertyChanged
                    this.PropertyChanged.Invoke(this,new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
        }
    }

    一个命令属性。。DelegateCommand  大家就在想这个DelegateCommand 是怎么来的呢,接着看下面代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Win32;
    using MVM.Commands;
    namespace MVM.ViewModels
    {
        public class MainWindowViewModel:NotificationObject
        {
            private double input1;
    
            public double Input1
            {
                get { return input1; }
                set
                {
                    input1 = value;
                    this.RaisePropertyChanged("Input1");
                }
            }
            private double input2;
    
            public double Input2
            {
                get { return input2; }
                set
                {
                    input2 = value;
                    this.RaisePropertyChanged("Input2");
                }
            }
    
            private double result;
            public double Result {
                get { return result; }
                set { 
                        result = value;
                        this.RaisePropertyChanged("Result");
                    }
            }
            //创建命令属性。。。
            public DelegateCommand AddCommand { get; set; }   
            //当AddCommand的Execute的时候,我们想要他做什么呢。。我们想要他做两个数据相加
            public void Add(object parameter) {
                Result = Input1 + Input2;
            }
            public DelegateCommand OpenCommand { get; set; }
            private void Open(object parameter)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.ShowDialog();
            }
    
            public MainWindowViewModel() {
                this.AddCommand = new DelegateCommand();
                this.AddCommand.ExecuteAction = new Action<object>(this.Add);
    
                this.OpenCommand = new DelegateCommand();
                this.OpenCommand.ExecuteAction = new Action<object>(this.Open);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;
    
    namespace MVM.Commands
    {
        public class DelegateCommand:ICommand
        {
            #region ICommand 成员
    
            public bool CanExecute(object parameter)
            {
                if (this.CanExecuteFunc == null) {
                    return true;
                }
                return this.CanExecuteFunc(parameter);
            }
    
            public event EventHandler CanExecuteChanged;
    
            /// <summary>
            /// 关键(命令执行时,你想做什么)
            /// </summary>
            /// <param name="parameter"></param>
            public void Execute(object parameter)
            {
                if (this.ExecuteAction == null) {
                    return;
                }
                this.ExecuteAction(parameter);
            }
            public Action<object> ExecuteAction { get; set; }
    
            public Func<object,bool> CanExecuteFunc { get; set; }
            #endregion
        }
    }

    界面,就更简单了。。

  • 相关阅读:
    java面试第八天
    java面试第七天
    java面试第六天
    java面试第五天
    java面试第四天
    SpringMVC导出Excel
    75. Autorelease机制及释放时机
    关于 SQLNET.AUTHENTICATION_SERVICES 验证方式的说明
    硬件十万个为什么——运放篇(五)PCB设计技巧
    eclipse到Android Studio的项目迁移
  • 原文地址:https://www.cnblogs.com/accpfriend/p/3477934.html
Copyright © 2011-2022 走看看