- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我读了一个MVVM tutorial我在命令部分迷路了。
using System;
using System.Windows.Input;
namespace MVVMDemo {
public class MyICommand : ICommand {
Action _TargetExecuteMethod;
Func<bool> _TargetCanExecuteMethod;
public MyICommand(Action executeMethod) {
_TargetExecuteMethod = executeMethod;
}
public MyICommand(Action executeMethod, Func<bool> canExecuteMethod){
_TargetExecuteMethod = executeMethod;
_TargetCanExecuteMethod = canExecuteMethod;
}
public void RaiseCanExecuteChanged() {
CanExecuteChanged(this, EventArgs.Empty);
}
bool ICommand.CanExecute(object parameter) {
if (_TargetCanExecuteMethod != null) {
return _TargetCanExecuteMethod();
}
if (_TargetExecuteMethod != null) {
return true;
}
return false;
}
// Beware - should use weak references if command instance lifetime
is longer than lifetime of UI objects that get hooked up to command
// Prism commands solve this in their implementation public event
EventHandler CanExecuteChanged = delegate { };
void ICommand.Execute(object parameter) {
if (_TargetExecuteMethod != null) {
_TargetExecuteMethod();
}
}
}
}
我不理解 RaiseCanExecuteChanged
方法和行 EventHandler CanExecuteChanged = delegate { };
的用途。我读到 EventHandler 是一个委托(delegate),但它是什么样的委托(delegate)呢?delegate { };
语句返回什么?
最佳答案
回答你的第二个问题,是的 EventHandler
是 void EventHandler(object sender, EventArgs args)
类型的委托(delegate)。以下行是分配给 EventHandler 的默认(空)匿名委托(delegate)。可能是为了防止 NullReferenceException
。下面一行也可以写成:
EventHandler canExecute = delegate { };
要了解 RaiseCanExecuteChanged
的用法,请阅读此处的答案:https://stackoverflow.com/a/4531378/881798
更新 -(@Will)
当要执行的命令的能力发生变化时, View 模型中的代码可以调用 RaiseCanExecuteChanged。例如,保存按钮在 UserName 和 Password 为空时返回 false,但当它们被填充时,VM 调用 RaiseCanExecuteChanged,当按钮询问命令是否可以触发时,它现在以肯定的方式响应。
关于c# - MVVM ICommand 和委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41109937/
我有一个针对 .NET Framework 4.0 的应用程序。它构建并运行良好,但我无法让它在 Dotfuscator CE 中编译,而且他们不会支持它,因为它是 CE。我被困住了。 Dotfusc
我的 View 模型中有一个 Icommand,如下所示: public ICommand SalaryCommand { get {
当我实现 ICommand接口(interface),创建以下方法 #region ICommand Members public bool CanExecute(object paramet
下面的代码是我的 WPF 应用程序的 MVVM 类。在我的 MainWindow.xaml.cs 文件 MainWindow() 构造函数中,我已经这样做了。 oneWayAuth = new One
我正在做以下 tutorial ,了解 WPF 中的 MVVM 模式。关于以下看似部分给出的 ICommand 接口(interface)实现,我有些不明白。 在下面的代码中,_canExecute
我定义了一个 ICommand 类 ReadPersons,它使用特定的 where 子句从数据库中读取所有人员对象。 通过按下按钮执行命令,并将 where 子句插入文本框。 问题:如何将文本框的文
我试图理解 ICommand 接口(interface)。我构建了一个带有按钮的应用程序,该按钮使用了一个名为 RelayCommand 的类,该类继承自 ICommand。该类看起来像这样:
在 WPF 中,我有一个名为 Malfunctions 的 ViewModel 类,它有一个 PartMalfunctions 的 ObservableCollection。通常,Observable
我是 C# 新手,这是我的第一个 WPF 项目。我正在关注 this tutorial (使用他们的 RelayCommand 实现并尝试使用 MVVM。我正在实现标准 Windows 计算器的克隆。
我有一个简单的按钮,它在执行时使用一个命令,一切正常,但我想在单击按钮时传递一个文本参数。 我认为我的 XAML 没问题,但我不确定如何编辑我的 RelayCommand 类以接收参数: publi
我有一个 UserControl,里面有一个按钮。此按钮需要将一些项目添加到所述 UC 内的网格中。我知道我可以通过 Click 事件来做到这一点。 这里的问题是我正在使用 MVVM,并且在相应的 V
我已经开始创建一个 wpf mvvm 应用程序。 ViewModel 的一个重要组成部分似乎是一堆 ICommand,它们具有允许 View 与 viewmodel 交互的松散耦合方式。 我的问题是,
我的 wpf-mvvm 应用程序中有一个按钮控件。 我使用 ICommand 属性(在 viewmodel 中定义)将按钮单击事件绑定(bind)到 viewmodel。 我的 ICommand 实现
在 WPF 中,使用 MVVM 设计,我创建了一个屏幕,用于将大量日志加载到 ListView 中。点击一个按钮。返回时,标签会更新以显示返回的日志数量。此过程有时可能需要一段时间。我们的 DA 正在
我正在为 Windows Store 应用程序应用 MVVM 模式(并在此过程中学习它)。 现在我倾向于在 View 和 ViewModel 之间建立 1:1 的对应关系,其中多个 ViewModel
我最近开始在 silverlight 中使用 MVVM 模式,但我不确定我是否正确使用它。 图形用户界面 我目前有一个包含股市板 block 组合框的 MainView。当用户选择一个部门(例如能源)
在我看来,我使用 ItemsControl 来显示几个按钮。 ItemsControl 的 XAML 是: 在我的
我有一个 ICommand实现是独立的。它修改有关实体的信息。同一实体绑定(bind)到 View ,作为 View 的 ViewModel 的属性. 我想要实现的是,在执行命令后,实体(以及因此是
让我们将按钮 Command 属性绑定(bind)到自定义命令。 何时应该实现 ICommand 以及何时从 RoatedCommand 派生?我看到 RoatedCommand 实现了 IComma
基本上,我有一个自定义控件FooControl。 public class FooControl : ItemsControl { //Code } 我需要添加一些事件处理,但与其使用 Rou
我是一名优秀的程序员,十分优秀!