本文介绍了MVVM光RelayCommand参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在与传递一个参数使用GalaSoft MVVM光框架relaycommand的问题。我知道MVVM光的实现relaycommand的不使用拉姆达的参数,所以我做了一些研究,发现了一种方法周围的人,它的工作做这样的事情:
I'm having an issue with passing a parameter to a relaycommand using the GalaSoft MVVM Light framework. I know that mvvm light's implementation of relaycommand doesn't use lambda parameters, so I did some research and found a way that people worked around it by doing something like this:
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;
}
不过,我一直就行了 _projmenuItem_Edit =新RelayCommand(ProjEditNode)得到一个错误;
,上面写着参数1:不能从转换'的方法组到System.Action
我在想什么?
推荐答案
我相信这将工作:
_projmenuItem_Edit = new RelayCommand<object>((txt)=>ProjEditNode(txt));
- 编辑 -
-- EDIT --
您需要定义与该类型的RelayCommand还有:
You'll need to define your RelayCommand with the type as well:
例如。
public RelayCommand<string> myCommand { get; private set; }
myCommand = new RelayCommand<string>((s) => Test(s));
private void Test(string s)
{
throw new NotImplementedException();
}
这篇关于MVVM光RelayCommand参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!