北京网站域名快速备案,百度推广是否做网站,wordpress图片怎么并排显示图片,wordpress的分享插件下载地址WPF的MVVM设计模式从winform转变到WPF的过程#xff0c;难点主要还是在MVVM的设计模式。当然#xff0c;如果依然采用winform的涉及方式#xff0c;在每个控件背后绑定事件的方式运用在wpf中#xff0c;依然可行#xff0c;但是假如GUI改版#xff0c;其背后绑定的特别为… WPF的MVVM设计模式从winform转变到WPF的过程难点主要还是在MVVM的设计模式。当然如果依然采用winform的涉及方式在每个控件背后绑定事件的方式运用在wpf中依然可行但是假如GUI改版其背后绑定的特别为此界面设计的事件不得不多数弃用。而MVVM最大的好处是将一切业务逻辑放在ViewModel中 将GUI的操作放在view中将数据结构放在Model中如图摘自MSDN实际使用使用了Prism框架省去了去构造实现INotifyPropertyChanged的基类直接继承BindableBasenamespace Prism.Mvvm{ // // 摘要: // Implementation of System.ComponentModel.INotifyPropertyChanged to simplify models. public abstract class BindableBase : INotifyPropertyChanged { protected BindableBase(); // // 摘要: // Occurs when a property value changes. public event PropertyChangedEventHandler PropertyChanged; // // 摘要: // Notifies listeners that a property value has changed. // // 参数: // propertyName: // Name of the property used to notify listeners. This value is optional and can // be provided automatically when invoked from compilers that support System.Runtime.CompilerServices.CallerMemberNameAttribute. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName null); // // 摘要: // Raises this objects PropertyChanged event. // // 参数: // propertyExpression: // A Lambda expression representing the property that has a new value. // // 类型参数: // T: // The type of the property that has a new value protected virtual void OnPropertyChangedT(ExpressionFuncT propertyExpression); // // 摘要: // Checks if a property already matches a desired value. Sets the property and notifies // listeners only when necessary. // // 参数: // storage: // Reference to a property with both getter and setter. // // value: // Desired value for the property. // // propertyName: // Name of the property used to notify listeners. This value is optional and can // be provided automatically when invoked from compilers that support CallerMemberName. // // 类型参数: // T: // Type of the property. // // 返回结果: // True if the value was changed, false if the existing value matched the desired // value. protected virtual bool SetPropertyT(ref T storage, T value, [CallerMemberName] string propertyName null); }以及用来构造Command的基类DelegateCommandnamespace Prism.Commands{ // // 摘要: // An System.Windows.Input.ICommand whose delegates do not take any parameters for // Prism.Commands.DelegateCommand.Execute and Prism.Commands.DelegateCommand.CanExecute. public class DelegateCommand : DelegateCommandBase { // // 摘要: // Creates a new instance of Prism.Commands.DelegateCommand with the System.Action // to invoke on execution. // // 参数: // executeMethod: // The System.Action to invoke when System.Windows.Input.ICommand.Execute(System.Object) // is called. public DelegateCommand(Action executeMethod); // // 摘要: // Creates a new instance of Prism.Commands.DelegateCommand with the System.Action // to invoke on execution and a Func to query for determining if the command can // execute. // // 参数: // executeMethod: // The System.Action to invoke when System.Windows.Input.ICommand.Execute(System.Object) // is called. // // canExecuteMethod: // The System.Func1 to invoke when System.Windows.Input.ICommand.CanExecute(System.Object) // is called public DelegateCommand(Action executeMethod, Funcbool canExecuteMethod); protected DelegateCommand(FuncTask executeMethod); protected DelegateCommand(FuncTask executeMethod, Funcbool canExecuteMethod); // // 摘要: // Factory method to create a new instance of Prism.Commands.DelegateCommand from // an awaitable handler method. // // 参数: // executeMethod: // Delegate to execute when Execute is called on the command. // // 返回结果: // Constructed instance of Prism.Commands.DelegateCommand public static DelegateCommand FromAsyncHandler(FuncTask executeMethod); // // 摘要: // Factory method to create a new instance of Prism.Commands.DelegateCommand from // an awaitable handler method. // // 参数: // executeMethod: // Delegate to execute when Execute is called on the command. This can be null to // just hook up a CanExecute delegate. // // canExecuteMethod: // Delegate to execute when CanExecute is called on the command. This can be null. // // 返回结果: // Constructed instance of Prism.Commands.DelegateCommand public static DelegateCommand FromAsyncHandler(FuncTask executeMethod, Funcbool canExecuteMethod); // // 摘要: // Determines if the command can be executed. // // 返回结果: // Returns true if the command can execute, otherwise returns false. public virtual bool CanExecute(); // // 摘要: // Executes the command. public virtual Task Execute(); // // 摘要: // Observes a property that is used to determine if this command can execute, and // if it implements INotifyPropertyChanged it will automatically call DelegateCommandBase.RaiseCanExecuteChanged // on property changed notifications. // // 参数: // canExecuteExpression: // The property expression. Example: ObservesCanExecute((o) PropertyName). // // 返回结果: // The current instance of DelegateCommand public DelegateCommand ObservesCanExecute(ExpressionFuncobject, bool canExecuteExpression); // // 摘要: // Observes a property that implements INotifyPropertyChanged, and automatically // calls DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. // // 参数: // propertyExpression: // The property expression. Example: ObservesProperty(() PropertyName). // // 类型参数: // T: // The object type containing the property specified in the expression. // // 返回结果: // The current instance of DelegateCommand public DelegateCommand ObservesPropertyT(ExpressionFuncT propertyExpression); }}第一次使用MVVM设计模式没有理解在多个ViewModel之间通信所以不得不采用单一ViewModel和多个view这样导致了一个ViewModel的臃肿和复杂看似结构简单但是实际的逻辑越来越混乱。在没有理解Event Aggregator如何使用的情况下这是可行方案。MVVM使用感受最主要的感受是MVVM将UI和业务逻辑分离UI就只写UI不用像WinForm一样在每个事件背后如果要获得UI某个TextBox的数据得通过如下获取public void SomeButton_Clicked(object sender, EventArgs e){ string text textBox1.Text; DoSomeThings(text); ...}同样后台事件要更新前台UI数据时pubic void SomeButton_Clicked(object sender, EvnetArgs e){ DoOtherThings(); textBox1.Text Some Text;}这种硬编码的形式遇到UI的重大变化必须就将背后事件对应UI的控件名称全部更改才能继续运行。当软件达到一定复杂度这样做就是灾难性的。而MVVM使用了数据绑定虽然增加了一点代码但是带来的好处巨大。在ViewModel中先定义要绑定的数据private string name;public string Name{ get{return name;} set{ if (name ! value) { name value; OnPropertyChanged(Name); // 实现INotifyPropertyChanged接口 }}}然后在view中将其和TextBox数据绑定TextBox Text{Binding Name, ModeTwoWay}这里的数据绑定方式是双向绑定后台数据变化会自动通知前台UI线程更新数据相反前台UI线程更改了数据后台的数据也会相应变化。这样在实际数据更新时不用去查看绑定的UI控件名称也不用担心在其他线程更新控件数据时要用oneControl.Invoke(Action action)。总结第一次使用MVVM感受到的优点数据绑定不用考虑具体UI控件的名称不用手动更新UI数据。UI可操作性更大支持template业务逻辑和UI分离界面改版方便但同样带来了缺点代码量明显增加对于小软件来说开发没有WinFrom来的敏捷