Sag*_*gio 45 c# wpf relaycommand mvvm-light
我在使用GalaSoft MVVM Light框架将参数传递给relaycommand时遇到问题.我知道mvvm light的relay命令的实现不使用lambda参数,所以我做了一些研究,并找到了一种方法,人们通过做这样的事情来解决它:
public RelayCommand ProjMenuItem_Edit
{
get
{
if (_projmenuItem_Edit == null)
{
//This should work....
_projmenuItem_Edit = new RelayCommand(ProjEditNode);
}
return _projmenuItem_Edit;
}
}
private void ProjEditNode(object newText)
{
var str = newText as string;
OrganLocationViewModel sel =
ProjectOrganLocationView.GetExtendedTreeView().GetTopNode();
//Console.WriteLine(sel.OrganDisplayName);
sel.OrganDisplayName = str;
}
Run Code Online (Sandbox Code Playgroud)
但是,我一直在_projmenuItem_Edit = new RelayCommand(ProjEditNode);说错误Argument 1: cannot convert from 'method group' to 'System.Action'
我错过了什么?
Rob*_*cus 84
我相信这会奏效:
_projmenuItem_Edit = new RelayCommand<object>((txt)=>ProjEditNode(txt));
Run Code Online (Sandbox Code Playgroud)
- 编辑 -
您还需要使用类型定义RelayCommand:
例如
public RelayCommand<string> myCommand { get; private set; }
myCommand = new RelayCommand<string>((s) => Test(s));
private void Test(string s)
{
throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)