MVVM Light Relay命令参数

发布于 2024-10-22 03:29:15 字数 823 浏览 2 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮光沉寂 2024-10-29 03:29:15

我相信这会起作用:

_projmenuItem_Edit = new RelayCommand<object>((txt)=>ProjEditNode(txt));

--编辑--

您还需要使用类型定义 RelayCommand:

例如

public RelayCommand<string> myCommand { get; private set; }
myCommand = new RelayCommand<string>((s) => Test(s));

private void Test(string s)
{
    throw new NotImplementedException();
}

I believe this will work:

_projmenuItem_Edit = new RelayCommand<object>((txt)=>ProjEditNode(txt));

-- EDIT --

You'll need to define your RelayCommand with the type as well:

e.g.

public RelayCommand<string> myCommand { get; private set; }
myCommand = new RelayCommand<string>((s) => Test(s));

private void Test(string s)
{
    throw new NotImplementedException();
}
夏见 2024-10-29 03:29:15

我认为 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 be RelayCommand<String> which would accept a method with void(string) signature. (and therefor would also be strongly typed and won't use the ugly object)

抠脚大汉 2024-10-29 03:29:15

另一种声明中继命令的方法将有助于减少代码

public RelayCommand ChartCommand
{
    set
    {
        RelayCommand<string> chartCommand = 
            new RelayCommand<string>(e => ExecuteChartCommand(e));               
    }
}

public void ExecuteChartCommand(string vendor)
{

}

Another way to declare relay commands, will help to reduce your code

public RelayCommand ChartCommand
{
    set
    {
        RelayCommand<string> chartCommand = 
            new RelayCommand<string>(e => ExecuteChartCommand(e));               
    }
}

public void ExecuteChartCommand(string vendor)
{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文