南昌专业做网站公司有哪些,代刷网站是怎么做的,php手机网站怎么做,江苏宿迁房产网这节我们介绍一下WPF中比较重要的接口ICommand#xff0c;也是WPF中一个新的特性#xff0c;做过WinForm朋友都知道#xff0c;WinForm开发是基于事件驱动开发模式#xff0c;比如一个Button有Click事件#xff0c;当我点击该按钮时#xff0c;在当前页面会执行具体的业务…这节我们介绍一下WPF中比较重要的接口ICommand也是WPF中一个新的特性做过WinForm朋友都知道WinForm开发是基于事件驱动开发模式比如一个Button有Click事件当我点击该按钮时在当前页面会执行具体的业务这样带来的UI和业务层产生紧密的耦合WPF也继承了WinForm这一旧的开发模式同时给我们添加了新的方法使得UI和后端业务完全隔离从而UI和业务逻辑解耦我们来看一下该接口的定义public interface ICommand{event EventHandler CanExecuteChanged;bool CanExecute(object parameter);void Execute(object parameter);}在整个MVVM架构中该接口起着非常重要的作用我们来看一下该接口成员CanExecuteChanged事件触发通知UI界面做出响应比如按钮禁用或启用表示CanExecute该接口返回一个bool值表示是否执行命令返回true命令执行false命令不执行。我们通过一个简单的例子实现命令绑定需求比较简单我们定义一个输入框如果为空状态提交按钮禁用如果有值按钮启用点击提交并将值显示到页面。我们定义一个DelegateCommand 实现ICommand接口public class DelegateCommand : ICommand{private Action _execute;private Funcbool _canExecute;public DelegateCommand(Action executeMethod){_execute executeMethod;}public DelegateCommand(Action executeMethod, Funcbool canExecute): this(executeMethod){this._canExecute canExecute;}public bool CanExecute(object parameter){return _canExecute();}public event EventHandler CanExecuteChanged {add{CommandManager.RequerySuggested value;}remove{CommandManager.RequerySuggested - value;}}public void Execute(object parameter){_execute();}}接下来我们定义一个ViewMode类命名为SampleViewModel实现INotifyPropertyChanged接口public class SampleViewModel : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;private void NotifyPropertyChanged([CallerMemberName] String propertyName ){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}private string name String.Empty;public string Name{get{return name;}set{if (value ! this.name){this.name value;NotifyPropertyChanged();}}}private string displayName string.Empty;public string DisplayName{get { return displayName; }set{if (value ! this.displayName){displayName value;NotifyPropertyChanged();}}}ICommand delegateCommand;public ICommand DelegateCommand{get{if (delegateCommand null){delegateCommand new DelegateCommand(Execute, CanExecute);}return delegateCommand;}}public void Execute(){DisplayName Name;}public bool CanExecute(){return !string.IsNullOrEmpty(Name);}} XMLA页面定义Window x:ClassExample_21.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:Example_21mc:IgnorabledTitleMainWindow Height450 Width800GridGrid.RowDefinitionsRowDefinition/RowDefinitionRowDefinition/RowDefinitionRowDefinition/RowDefinition/Grid.RowDefinitionsTextBox Grid.Row0 NametxtBox Width200 Text{Binding PathName,UpdateSourceTriggerPropertyChanged} VerticalAlignmentCenter HorizontalAlignmentCenter/TextBlock Grid.Row1 Width200 Text{Binding PathDisplayName}/TextBlockButton Grid.Row2 x:UidbtnSend x:NamebtnSend FontSize16 Content提交 Width100 Height50 Margin0,0,0,5 Command{Binding DelegateCommand} /Button/Grid
/WindowMainWindow.xaml.cs 文件public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext new SampleViewModel();}}从上面的例子可以看出我们可以使用控件的Command属性来绑定要执行的命令我们还可以设置CommandParameter来传递参数可以把UI上数据传递到后台WPF整个框架是基于数据驱动我们可以做到把UI层完全剥离出来所以说WPF是一个前端和后台可以完全分离的框架。其实走到这里结合我们WPF-18 INotifyPropertyChanged 接口我们有点看到了MVVM开发模式的影子。接下来我们使用MVVM模式做一个简单的Demo