WPF比较两个随机数大小写,利用MVVM思想实现
MVVM模式是把表现层和业务层完全分离,所以这里就使用MVVM制作一个极其简单的WPF的例子:
先看看最终图:
上图,需要实现的是,界面两个按钮,,一个是生成随机两个数,一个是对两个数比较,把大的数显示出来。所以需要三个属性,两个事件。
由于逻辑比较简单,不用写model等类,实现如下:
1、创建基类NotifictionObject.cs,实现接口INotifyPropertyChanged
class NotifictionObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string str) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(str)); } }
2、创建DelegateCommand.cs,继承ICommand
class DelegateCommand : ICommand { public Func<object,bool> CanExecuteFunc { get; set; } public Action<object> ExecuteAction { get; set; } public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { if (CanExecuteFunc == null) return true; return CanExecuteFunc(parameter); } public void Execute(object parameter) { if (ExecuteAction == null) return; ExecuteAction(parameter); } }
3、再创建类MainWindowViewModel.cs,继承NotifictionObject基类
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67479.html