MVVM Light Relay命令参数
我在使用 GalaSoft MVVM Light 框架将参数传递给中继命令时遇到问题。我知道 mvvm light 的relaycommand 实现不使用 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;
}
但是,我不断收到错误 _projmenuItem_Edit = new RelayCommand(ProjEditNode);
上面写着参数 1:无法从“方法组”转换为“System.Action”
我缺少什么?
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;
}
However, I keep getting an error on the line _projmenuItem_Edit = new RelayCommand(ProjEditNode);
that says Argument 1: cannot convert from 'method group' to 'System.Action'
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这会起作用:
--编辑--
您还需要使用类型定义 RelayCommand:
例如
I believe this will work:
-- EDIT --
You'll need to define your RelayCommand with the type as well:
e.g.
我认为
RelayCommand()
没有一个非空的构造函数。你试图向它传递错误的方法。如果您希望 RelayCommand 支持命令参数,则应使用
RelayCommand
,其中 T 可以是任何类型的参数。在您的情况下,它将是RelayCommand
,它将接受带有 void(string) 签名的方法。 (因此也将是强类型的并且不会使用丑陋的对象)I don't think that
RelayCommand()
has a constructor that is not empty. you're trying to pass the wrong kind of method to it.If you want the RelayCommand to support Command Parameters, you should use
RelayCommand<T>
where T can be any type of parameter. In your situation it would beRelayCommand<String>
which would accept a method with void(string) signature. (and therefor would also be strongly typed and won't use the ugly object)另一种声明中继命令的方法将有助于减少代码
Another way to declare relay commands, will help to reduce your code