MVVM 命令错误

本文关键字:错误 命令 MVVM | 更新日期: 2023-09-27 18:34:34

我对WPF MVVM模式非常陌生,尤其是commands

包含命令的ViewModel

public ICommand MyButtonClickCommand
{
    get { return new RelayCommand(FuncToCall); }
}
private object FuncToCall(object context)
{
    MessageBox.Show("The button was Clicked", "");
    return null;
}

RelayCommandClass.cs

private Action<object> _action;
public RelayCommand(Action<object> action)
{
    _action = action;
}

在编译时,我得到了FuncToCall(object)' has the wrong return type

谢谢大家

MVVM 命令错误

Visual Studio在编写方法名称并单击其建议以为您创建方法时会添加错误的方法签名。将签名更改为此签名,一切都会好起来的:

private void FuncToCall(object context)
{
    MessageBox.Show("The button was Clicked", "");
}

使 FuncToCall(( 返回类型Void并删除return语句。

private void FuncToCall(object context)
{
    MessageBox.Show("The button was Clicked", "");
}

FuncToCall 方法不应返回任何内容。

FuncToCall(( 方法不应该任何返回类型并删除 return 语句。