当前位置: 首页 > wzjs >正文

长春建设银行网站溧阳人才网 网站开发

长春建设银行网站,溧阳人才网 网站开发,网站开发协议模板,用ps做网站设计命令系统(ICommand) 1 RelayCommand实现2 CanExecute控制按钮可用性3 参数传递(CommandParameter)3.1 静态参数绑定:3.2 动态参数绑定:3.3 复杂对象参数: 4 异步命令实现5 常见问题排查 WPF的命…

命令系统(ICommand)

    • 1 RelayCommand实现
    • 2 CanExecute控制按钮可用性
    • 3 参数传递(CommandParameter)
      • 3.1 静态参数绑定:
      • 3.2 动态参数绑定:
      • 3.3 复杂对象参数:
    • 4 异步命令实现
    • 5 常见问题排查

WPF的命令系统是MVVM模式中实现业务逻辑与UI交互的核心机制。本章将深入解析 ICommand接口的实现原理,并提供企业级应用中的最佳实践方案。

1 RelayCommand实现

通过自定义命令类解耦UI与业务逻辑:

基础实现模板:

public class RelayCommand : ICommand
{private readonly Action _execute;private readonly Func<bool> _canExecute;public RelayCommand(Action execute, Func<bool> canExecute = null){_execute = execute ?? throw new ArgumentNullException(nameof(execute));_canExecute = canExecute;}public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true;public void Execute(object parameter) => _execute();public event EventHandler CanExecuteChanged{add => CommandManager.RequerySuggested += value;remove => CommandManager.RequerySuggested -= value;}
}// 支持泛型参数的增强版
public class RelayCommand<T> : ICommand
{private readonly Action<T> _execute;private readonly Func<T, bool> _canExecute;public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null){_execute = execute ?? throw new ArgumentNullException(nameof(execute));_canExecute = canExecute;}public bool CanExecute(object parameter) => _canExecute?.Invoke((T)parameter) ?? true;public void Execute(object parameter) => _execute((T)parameter);public event EventHandler CanExecuteChanged{add => CommandManager.RequerySuggested += value;remove => CommandManager.RequerySuggested -= value;}
}

ViewModel中的使用示例:

public class MainViewModel
{public RelayCommand SaveCommand { get; }public RelayCommand<string> SearchCommand { get; }public MainViewModel(){SaveCommand = new RelayCommand(ExecuteSave, CanSave);SearchCommand = new RelayCommand<string>(ExecuteSearch);}private void ExecuteSave() => /* 保存逻辑 */;private bool CanSave() => !string.IsNullOrEmpty(Content);private void ExecuteSearch(string keyword) => /* 搜索逻辑 */;
}

2 CanExecute控制按钮可用性

命令的可用性状态与UI元素自动同步:

XAML绑定示例:

<Button Content="保存" Command="{Binding SaveCommand}"IsEnabled="{Binding SaveCommand.IsEnabled}"/>

动态更新策略:

  1. 自动更新(默认):
// 通过CommandManager自动触发
CommandManager.InvalidateRequerySuggested();
  1. 手动通知:
// 在属性变更时触发
public string Content
{set {_content = value;OnPropertyChanged();SaveCommand.RaiseCanExecuteChanged();}
}

禁用状态样式优化:

<Style TargetType="Button"><Style.Triggers><Trigger Property="IsEnabled" Value="False"><Setter Property="Opacity" Value="0.5"/></Trigger></Style.Triggers>
</Style>

3 参数传递(CommandParameter)

支持多种参数传递方式:

3.1 静态参数绑定:

<Button Command="{Binding StatusCommand}" CommandParameter="Approved"/>

3.2 动态参数绑定:

<ComboBox x:Name="statusList" SelectedValuePath="Tag"/>
<Button Command="{Binding UpdateCommand}" CommandParameter="{Binding SelectedItem.Tag, ElementName=statusList}"/>

3.3 复杂对象参数:

// ViewModel
public RelayCommand<User> EditCommand { get; } = new RelayCommand<User>(user => /* 编辑逻辑 */);
// XAML
<ListBox x:Name="userList"><ListBox.ItemTemplate><DataTemplate><Button Content="编辑" Command="{Binding DataContext.EditCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"CommandParameter="{Binding}"/></DataTemplate></ListBox.ItemTemplate>
</ListBox>

4 异步命令实现

处理长时间运行任务的最佳实践:

异步命令模板:

public class AsyncCommand : ICommand
{private readonly Func<Task> _execute;private readonly Func<bool> _canExecute;private bool _isExecuting;public AsyncCommand(Func<Task> execute, Func<bool> canExecute = null){_execute = execute;_canExecute = canExecute;}public bool CanExecute(object parameter) => !_isExecuting && (_canExecute?.Invoke() ?? true);public async void Execute(object parameter){if (CanExecute(parameter)){try{_isExecuting = true;RaiseCanExecuteChanged();await _execute();}finally{_isExecuting = false;RaiseCanExecuteChanged();}}}public void RaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();public event EventHandler CanExecuteChanged{add => CommandManager.RequerySuggested += value;remove => CommandManager.RequerySuggested -= value;}
}

使用示例:

public AsyncCommand LoadDataCommand { get; }public MainViewModel()
{LoadDataCommand = new AsyncCommand(LoadDataAsync, () => !IsLoading);
}private async Task LoadDataAsync()
{IsLoading = true;try{await DataService.FetchData();}finally{IsLoading = false;}
}

5 常见问题排查

问题1:命令不触发

  • 检查CanExecute返回值是否为true
  • 确认DataContext是否正确继承
  • 验证参数类型匹配(使用RelayCommand<T>时)

问题2:CanExecute不自动更新

  • 确保调用CommandManager.InvalidateRequerySuggested()
  • 检查是否在属性变更时触发通知
  • 对于非UI线程更新,使用Dispatcher调用:
Application.Current.Dispatcher.Invoke(CommandManager.InvalidateRequerySuggested);

问题3:参数绑定失败

  • 使用调试转换器检查参数值:
<Button CommandParameter="{Binding SelectedItem, Converter={local:DebugConverter}}"/>
  • 确认参数类型与命令泛型类型匹配

问题4:内存泄漏

  • 及时取消命令订阅:
public void Dispose()
{SaveCommand.CanExecuteChanged -= OnSaveCommandChanged;
}

本章小结
通过本章学习,开发者应掌握:

  • 实现符合生产标准的RelayCommand
  • 通过CanExecute控制UI状态
  • 多种参数传递模式的应用
  • 异步命令的安全实现
  • 常见命令问题的诊断方法

建议实践以下场景:

  • 开发带撤销/重做功能的编辑器
  • 实现分页数据加载命令
  • 创建支持多选操作的批量处理命令

下一章将深入讲解MVVM模式的核心架构与实现细节。


文章转载自:

http://g1FDCLXq.Lqznq.cn
http://lplf2w4G.Lqznq.cn
http://fXZj4Qgx.Lqznq.cn
http://P2Lkemvh.Lqznq.cn
http://dCCXwqO8.Lqznq.cn
http://uyw7Lwa6.Lqznq.cn
http://qcLX5xeC.Lqznq.cn
http://bAxl17Yz.Lqznq.cn
http://EJxtGbG7.Lqznq.cn
http://Lne2uto8.Lqznq.cn
http://VmGP5fXT.Lqznq.cn
http://XQjSxk7W.Lqznq.cn
http://P1hhrmfE.Lqznq.cn
http://93nw1C2G.Lqznq.cn
http://tNh35ofB.Lqznq.cn
http://dXl0LIZk.Lqznq.cn
http://FGRT5MM9.Lqznq.cn
http://IhxwJbVS.Lqznq.cn
http://cgrqea5e.Lqznq.cn
http://TbuDrywm.Lqznq.cn
http://pAq8ZpmB.Lqznq.cn
http://5Fuh887j.Lqznq.cn
http://XvwdnefQ.Lqznq.cn
http://oBdSmNrl.Lqznq.cn
http://i7WvAzTj.Lqznq.cn
http://amC37Txr.Lqznq.cn
http://YefF3Gki.Lqznq.cn
http://qus0LZ3E.Lqznq.cn
http://Hure2vcx.Lqznq.cn
http://6LsAHSV9.Lqznq.cn
http://www.dtcms.com/wzjs/727976.html

相关文章:

  • 做网站安全的公司有哪些北京模板网站开发公司
  • 网站建设组织专注高密做网站哪家好
  • 个人介绍微电影网站模板软文案例大全
  • 网站建设正规公司iis 建立子网站
  • 长春新建高铁站网站安全狗 fastcgi
  • 建站工具官网重庆建设摩托车价格及图片
  • 七台河网站网站建设深圳展示型网站建设
  • ucenter整合wordpress白山网站seo
  • 怎样设置自己的网站免费培训seo
  • 如何利用fortran语言建设网站又拍云wordpress全站cdn
  • c2c跨境电子商务平台湖南seo推广软件
  • 支付网站建设费用做账网站不让百度收录
  • 深圳网站程序开发深圳10大产品设计公司
  • 网站建设与管理吴振峰ppt关键词推广平台
  • 大连开发区做网站的公司点卡平台网站开发
  • 重庆模板网站建设费用漫画网站源码
  • 北京酷站科技有限公司秦皇岛市建设局
  • 学做企业网站网站建设与维护试卷 一
  • 短网址在线生成免费佛山网站优化
  • 龙岗区住房和建设局官方网站八大员考试报名官网
  • wordpress 网站换域名asp个人网站论文
  • 网站建设重要意义深一互联网站建设怎样
  • 设计公司网站制作淘客网站怎么建设
  • 做产品网站架构图之前注意要点个人网站优秀
  • 辽宁鞍山网站建设公司计算机专业主要学什么科目
  • 仙居网站建设网站建设的一般流程是什么意思
  • 网页设计商城网站建设企业做网站的公司
  • form e哪个网站做深圳网页设计招聘信息
  • 做网站准备什么软件网站怎么做用qq登录接入
  • 怎么样给一个网站做自然排名网站被黑怎么办