使用 DelegateCommand 出现 Specified cast is not valid 最近写快捷键需要 DelegateCommand ,于是用了 DelegateCommand<double > ,运行时出现 Specified cast is not valid 原因是 DelegateCommand 传入的 Object 是可空的,如果使用 Double ,那么是不可空的,就出现错误 于是就可以啦 如果遇到 DelegateCommand 出现这个错误,一般就是使用不可空的类型,只要让他可空就好。
_loadModulesCommand; private DelegateCommand _openViewA; private DelegateCommand _openViewB ; private DelegateCommand _goBackView; private DelegateCommand _goForwardView; (_openViewA = new DelegateCommand(OpenViewAAction)); } public DelegateCommand OpenViewB (_openViewB = new DelegateCommand(OpenViewBAction)); } public DelegateCommand GoBackView (_goBackView = new DelegateCommand(GoBackViewAction)); } public DelegateCommand GoForwardView
(_loadingCommand = new DelegateCommand(ExecuteLoadingCommand)); private DelegateCommand _activePaientListCommand (_activePaientListCommand = new DelegateCommand(ExecuteActivePaientListCommand)); private DelegateCommand (_deactivePaientListCommand = new DelegateCommand(ExecuteDeactivePaientListCommand)); private DelegateCommand (() => IsCanExcute)); private DelegateCommand _deactiveMedicineListCommand; public DelegateCommand (() => IsCanExcute)); private DelegateCommand _loadMedicineModuleCommand; public DelegateCommand
GetMessageCommand { get => _getMessageCommand = new DelegateCommand(() => CancelMessageCommand { get => _cancelMessageCommand = new DelegateCommand(() = _loadModulesCommand; private DelegateCommand _showDialogCommand; public IView { get => _loadModulesCommand = new DelegateCommand(InitModules); } public IModuleInfo ModuleInfo ShowDialogCommand { get => _showDialogCommand = new DelegateCommand(ShowDialogAction); } //
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的命令的用法 一.创建DelegateCommand命令 我们在上一篇.NET Core 3 WPF MVVM框架 Prism 二.创建DelegateCommand带参命令 在创建带参的命令之前,我们可以来看看DelegateCommand的继承链和暴露出来的公共方法,详细的实现可以去看下源码 ? 那么,其实已经很明显了,我们之前创建DelegateCommand不是泛型版本,当创建一个泛型版本的DelegateCommand<T>,那么T就是我们要传入的命令参数的类型,那么,我们现在可以把触发命令的 _asyncCommand; public DelegateCommand AsyncCommand => _asyncCommand ?? ; } ); } 也可以更简洁的写法: private DelegateCommand _asyncCommand; public DelegateCommand AsyncCommand
_loginLoadingCommand; public DelegateCommand LoginLoadingCommand => _loginLoadingCommand (_createAccountCommand = new DelegateCommand(ExecuteCreateAccountCommand)); //导航到CreateAccount (_loginMainContentCommand = new DelegateCommand(ExecuteLoginMainContentCommand)); private DelegateCommand private DelegateCommand _goForwardCommand; public DelegateCommand GoForwardCommand => _goForwardCommand _goBackCommand; public DelegateCommand GoBackCommand => _goBackCommand ??
的内置Command还有需要许多不足,因此在模式与实践团队的Prism项目中也打造一套自己的Command,特别是与UI元素耦合以及不支持命令组合,所以他们在Prism中便增加了另外一套Command:DelegateCommand DelegateCommand:实现了WPF/Silverlight的ICommand接口,仍只支持一个CanExecute和Execute挂接,但其实现一个称为IActiveAware的接口用于指示是否处于集合状态 ,非激活状态的DelegateCommand始终得不到执行。 CompositeCommand:也是WPF/Silverlight的ICommand接口的一个实现,但其同时也是DelegateCommand的组合,可以向其中注册或取消注册DelegateCommand ,当其中所有处于激活状态的内置DelegateCommand都可以被执行时其CanExecute才返回true。
<Patient> _mouseDoubleClickCommand; public DelegateCommand<Patient> MouseDoubleClickCommand => (_mouseDoubleClickCommand = new DelegateCommand<Patient>(ExecuteMouseDoubleClickCommand)); IEventAggregator <Medicine> _addMedicineCommand; public DelegateCommand<Medicine> AddMedicineCommand => _addMedicineCommand (_addMedicineCommand = new DelegateCommand<Medicine>(ExecuteAddMedicineCommand)); public SearchMedicineViewModel _cancleSubscribeCommand; public DelegateCommand CancleSubscribeCommand => _cancleSubscribeCommand
Commanding ICommand 同样是 MVVM 模式的核心元素,DelegateCommand 实现了 ICommand 接口,它最基本的使用形式如下,其中 DelegateCommand 构造函数中的第二个参数 canExecuteMethod 是可选的: public DelegateCommand SubmitCommand { get; private set; } public CanExecute 返回 DelegateCommand 构造函数中的第二个参数 canExecuteMethod 的返回值。如果不传入这个参数,则 CanExecute 一直返回 True。 除了主动调用 RaiseCanExecuteChanged,DelegateCommand 还可以用 ObservesProperty 和 ObservesCanExecute 两种形式监视属性,定于属性的 例如,安装此工具后可以通过 cmd 代码段快速生成一个完整的 DelegateCommand 代码: private DelegateCommand _fieldName; public DelegateCommand
WpfApp11.Model; namespace mvvm实例1.ViewModel { public class StudentViewModel { public DelegateCommand public StudentViewModel() { Student = new StudentModel(); ShowCommand = new DelegateCommand Student.StudentEmail = "438679770@qq.com"; Student.StudentSex = "男"; } } public class DelegateCommand CanExecuteChanged(this, EventArgs.Empty); } } } } 代码中,除了定义StudentViewModel类外,还定义了DelegateCommand 我们可以将实现了ICommand接口的命令DelegateCommand赋值给Button(命令源)的Command属性(只有实现了ICommandSource接口的元素才拥有该属性),这样Button
get { return _text; } set { SetProperty(ref _text, value); } } private DelegateCommand _clickCommnd; public DelegateCommand ClickCommnd => _clickCommnd ?? (_clickCommnd = new DelegateCommand(ExecuteClickCommnd)); void ExecuteClickCommnd() _clickCommnd; public DelegateCommand ClickCommnd => _clickCommnd ?? (_clickCommnd = new DelegateCommand(ExecuteClickCommnd)); void ExecuteClickCommnd()
private DelegateCommand _printMsg1Command; public DelegateCommand PrintMsg1Command { get => _printMsg1Command (_printMsg1Command = new DelegateCommand(PrintMsgAction)); } 实现command需要执行的内容。 private DelegateCommand _printMsg1Command; private DelegateCommand _printMsg2Command; public DelegateCommand (_printMsg1Command = new DelegateCommand(PrintMsgAction1)); } public DelegateCommand PrintMsg2Command (_printMsg2Command = new DelegateCommand(PrintMsgAction2)); } private void PrintMsgAction1() {
DelegateCommand.cs using System; using System.Collections.Generic; using System.Linq; using System.Text value; this.RaisePropertyChange("Result"); } } public DelegateCommand this.Input2; } public MainWindowViewModel() { this.AddCommand = new DelegateCommand value; this.RaisePropertyChange("Result"); } } public DelegateCommand this.Input2; } public MainWindowViewModel() { this.AddCommand = new DelegateCommand
> </StackPanel> public MenuItemSamplePage() { this.InitializeComponent(); var command = new DelegateCommand string text = parameter as string; return string.IsNullOrWhiteSpace(text) == false; } 这里用到的DelegateCommand 也是UWPCommunityToolkit中的类 :DelegateCommand
而我的业务是要右击打开下载项的文件夹或文件,此时的数据可以通过对应行的数据拿到 在 ContextMenu 的菜单里面需要绑定命令,而默认的命令不够好用,咱先磨一下刀,新建一个类,请看代码 public class DelegateCommand CanExecuteChanged; } 通过这个类就可以在 XAML 写绑定命令的资源和代码,请看代码 <local:DelegateCommand x:Key="OpenFileCommand " ExecuteDelegate="OpenFileCommand_OnExecute" ></local:DelegateCommand> <local:DelegateCommand x:Key ="OpenFolderCommand" ExecuteDelegate="OpenFolderCommand_OnExecute" ></local:DelegateCommand> 记得在 cs 代码里面添加对应的
Change Convention 更改ViewModelLocator命名约定 ViewModelLocator - Custom Registrations 为特定视图手动注册ViewModels DelegateCommand 使用DelegateCommand和DelegateCommand<T> CompositeCommands 了解如何使用CompositeCommands作为单个命令调用多个命令 IActiveAware return _message; } set { SetProperty(ref _message, value); } } public DelegateCommand MessageViewModel(IEventAggregator ea) { _ea = ea; SendMessageCommand = new DelegateCommand
双击托盘运行代码需要添加命令,创建一个 ViewModel 用来绑定命令 public class DelegateCommand : ICommand { public public ICommand ShowWindowCommand { get { return new DelegateCommand public ICommand HideWindowCommand { get { return new DelegateCommand ICommand ExitApplicationCommand { get { return new DelegateCommand
Microsoft YaHei;font-size:14px;">class MainWindowViewModel : NotificationObject { //通过委托形式,定义命令属性 public DelegateCommand PlaceOrderCommand { get; set; } public DelegateCommand SelectMenuItemCommand { get; set; } private this.LoadRestaurant(); this.LoadDishMenu(); //单向“命令属性”的加载过程,通过实例化委托的形式 this.PlaceOrderCommand = new DelegateCommand (new Action(this.PlaceOrderCommandExecute)); this.SelectMenuItemCommand = new DelegateCommand(new Action ) { this.Count = this.DishMenu.Count(i => i.IsSelected == true); } } 这个"栗子"当中,发现“public DelegateCommand
this.AllColors = new[] { "Red", "Blue", "Green" }; 12 13 this.SubmitCommand = new DelegateCommand <object>(this.OnSubmit);//实例化一个command,DelegateCommand引用类库 Microsoft.Practices.Prism.Commands 14 其中Button控件绑定了无参数的ICommand命令,后台为DelegateCommand。通过Button控件 我们可以获取到数据源的变化,并将它显示到页面上。。
MainWindowViewModel(IModuleManager moduleManager) { _moduleManager = moduleManager; } private DelegateCommand _loadPatientModuleCommand; public DelegateCommand LoadPatientModuleCommand => _loadPatientModuleCommand (_loadPatientModuleCommand = new DelegateCommand(ExecuteLoadPatientModuleCommand)); void ExecuteLoadPatientModuleCommand