C# 重用RelayCommand的代码

C# 重用RelayCommand的代码,c#,wpf,mvvm,mvvm-light,relaycommand,C#,Wpf,Mvvm,Mvvm Light,Relaycommand,我将MVVM light用于WPF应用程序。我有一个视图模型,其中包含几个使用RelayCommand的命令。因为每个命令的代码都非常相似,所以我创建了一个GetCommand方法。但是,如果在RelayCommand中使用参数,则生成的RelayCommand不起作用。如果我不使用param,一切正常(除了我不能传递值) 有人能解释一下为什么会发生这种情况,还有什么其他解决方案可以在不复制粘贴的情况下重用代码 下面是我的代码的简化版本,只显示了重要部分: public class MainVi

我将MVVM light用于WPF应用程序。我有一个视图模型,其中包含几个使用RelayCommand的命令。因为每个命令的代码都非常相似,所以我创建了一个GetCommand方法。但是,如果在RelayCommand中使用参数,则生成的RelayCommand不起作用。如果我不使用param,一切正常(除了我不能传递值)

有人能解释一下为什么会发生这种情况,还有什么其他解决方案可以在不复制粘贴的情况下重用代码

下面是我的代码的简化版本,只显示了重要部分:

public class MainViewModel {
   public RelayCommand commandOne = GetCommand("one");
   public RelayCommand commandTwo = GetCommand("two");

   public RelayCommand GetCommand(string param) {
      return new RelayCommand(() => {
         // Do something accessing other properties of MainViewModel
         // to detect if another action is alreay running
         // this code would need to be copy & pasted everywhere
         if(param == "one")
            _dataService.OneMethod();
         else if(param == "two")
            _dataService.TwoMethod();
         else
            _dataService.OtherMethod();
         var name = param;
      });
   }
}

这就是我通常使用RelayCommands的方式,我只是将命令绑定到方法

public class MainViewModel {
    public MainViewModel()
    {
        CommandOne = new RelayCommand<string>(executeCommandOne);
        CommandTwo = new RelayCommand(executeCommandTwo);
    }

    public RelayCommand<string> CommandOne { get; set; }

    public RelayCommand CommandTwo { get; set; }

    private void executeCommandOne(string param)
    {
        //Reusable code with param
    }

    private void executeCommandTwo()
    {
        //Reusable code without param
    }
}
public类主视图模型{
公共主视图模型()
{
CommandOne=新的RelayCommand(executeCommandOne);
CommandTwo=新的RelayCommand(executeCommandTwo);
}
公共RelayCommand命令{get;set;}
公共RelayCommand命令二{get;set;}
私有void executeCommandOne(字符串参数)
{
//带param的可重用代码
}
私有void executeCommandTwo()
{
//无参数的可重用代码
}
}

这就是我通常使用RelayCommands的方式,我只是将命令绑定到方法

public class MainViewModel {
    public MainViewModel()
    {
        CommandOne = new RelayCommand<string>(executeCommandOne);
        CommandTwo = new RelayCommand(executeCommandTwo);
    }

    public RelayCommand<string> CommandOne { get; set; }

    public RelayCommand CommandTwo { get; set; }

    private void executeCommandOne(string param)
    {
        //Reusable code with param
    }

    private void executeCommandTwo()
    {
        //Reusable code without param
    }
}
public类主视图模型{
公共主视图模型()
{
CommandOne=新的RelayCommand(executeCommandOne);
CommandTwo=新的RelayCommand(executeCommandTwo);
}
公共RelayCommand命令{get;set;}
公共RelayCommand命令二{get;set;}
私有void executeCommandOne(字符串参数)
{
//带param的可重用代码
}
私有void executeCommandTwo()
{
//无参数的可重用代码
}
}

您可能正在寻找以下内容

public partial class MainWindow : Window
{
    private RelayCommand myRelayCommand ;
    private string param = "one";

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public RelayCommand MyRelayCommand 
    {
        get
        {
            if (myRelayCommand == null)
            {
                myRelayCommand = new RelayCommand((p) => { ServiceSelector(p); });
            }
            return myRelayCommand;
        }
    }

    private void DoSomething()
    {
        MessageBox.Show("Did Something");
    }

    private void ServiceSelector(object p)
    {
        DoSomething();

        if (param == "one")
            MessageBox.Show("one");
        else if (param == "two")
            MessageBox.Show("two");
        else
            MessageBox.Show("else");
        var name = param;
    }
}

您可能正在寻找以下内容

public partial class MainWindow : Window
{
    private RelayCommand myRelayCommand ;
    private string param = "one";

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public RelayCommand MyRelayCommand 
    {
        get
        {
            if (myRelayCommand == null)
            {
                myRelayCommand = new RelayCommand((p) => { ServiceSelector(p); });
            }
            return myRelayCommand;
        }
    }

    private void DoSomething()
    {
        MessageBox.Show("Did Something");
    }

    private void ServiceSelector(object p)
    {
        DoSomething();

        if (param == "one")
            MessageBox.Show("one");
        else if (param == "two")
            MessageBox.Show("two");
        else
            MessageBox.Show("else");
        var name = param;
    }
}

但这适用于运行时需要参数的情况。我只是想从dataService调用一个不同的方法,但在这样做之前,我需要运行一些与每个命令相同的代码。我想我不是真的理解你在寻找什么,你不能将重复的代码放入一个方法中吗?但这适用于运行时需要参数的情况。我只是想从dataService调用一个不同的方法,但在这样做之前,我需要运行一些与每个命令相同的代码。我想我不是真的理解你在寻找什么,你不能将重复的代码放入一个方法中吗?当你说“.生成的RelayCommand不起作用”,是命令没有开火还是其他什么东西不起作用。我在一个示例应用程序中尝试了类似的代码,这些命令对我很有效。当你说“.生成的RelayCommand不起作用”时,是命令没有启动还是其他东西不起作用。我在一个示例应用程序中尝试了类似的代码,这些命令对我很有用。