使用 ViewModel 中定义的 RelayCommand 传递参数(来自 Josh Smith 示例) [英] Passing a parameter using RelayCommand defined in the ViewModel (from Josh Smith example)

查看:13
本文介绍了使用 ViewModel 中定义的 RelayCommand 传递参数(来自 Josh Smith 示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 RelayCommand 将在我的应用程序的 XAML(视图)中定义的参数传递给 ViewModel 类.我关注了 Josh Smith 关于 MVVM 的优秀文章 并实现了以下内容.

I would like to pass a parameter defined in the XAML (View) of my application to the ViewModel class by using the RelayCommand. I followed Josh Smith's excellent article on MVVM and have implemented the following.

XAML 代码

        <Button 
        Command="{Binding Path=ACommandWithAParameter}"
        CommandParameter="Orange"
        HorizontalAlignment="Left" 
        Style="{DynamicResource SimpleButton}" 
        VerticalAlignment="Top" 
        Content="Button"/>

视图模型代码

  public RelayCommand _aCommandWithAParameter;
  /// <summary>
  /// Returns a command with a parameter
  /// </summary>
  public RelayCommand ACommandWithAParameter
  {
     get
     {
        if (_aCommandWithAParameter == null)
        {
           _aCommandWithAParameter = new RelayCommand(
               param => this.CommandWithAParameter("Apple")
               );
        }

        return _aCommandWithAParameter;
     }
  }

  public void CommandWithAParameter(String aParameter)
  {
     String theParameter = aParameter;
  }
  #endregion

我在 CommandWithAParameter 方法中设置了一个断点,并观察到 ​​aParameter 设置为Apple",而不是Orange".这似乎很明显,因为使用文字字符串Apple"调用方法 CommandWithAParameter.

I set a breakpoint in the CommandWithAParameter method and observed that aParameter was set to "Apple", and not "Orange". This seems obvious as the method CommandWithAParameter is being called with the literal String "Apple".

但是,查找执行堆栈,可以看到Orange",我在XAML 中设置的CommandParameter 是ICommand Execute 接口方法的RelayCommand 实现的参数值.

However, looking up the execution stack, I can see that "Orange", the CommandParameter I set in the XAML is the parameter value for RelayCommand implemenation of the ICommand Execute interface method.

即执行栈下面方法中的参数值为Orange",

That is the value of parameter in the method below of the execution stack is "Orange",

  public void Execute(object parameter)
  {
     _execute(parameter);
  }

我想弄清楚的是如何创建 RelayCommand ACommandWithAParameter 属性,以便它可以使用 XAML 中定义的 CommandParameterOrange"调用 CommandWithAParameter 方法.

What I am trying to figure out is how to create the RelayCommand ACommandWithAParameter property such that it can call the CommandWithAParameter method with the CommandParameter "Orange" defined in the XAML.

有没有办法做到这一点?

Is there a way to do this?

我为什么要这样做?即时本地化"的一部分在我的特定实现中,我想创建一个可以绑定到多个按钮的 SetLanguage RelayCommand.我想将两个字符的语言标识符(en"、es"、ja"等)作为 CommandParameter 传递,并为 XAML 中定义的每个设置语言"按钮定义它.我想避免必须为每种支持两个字符的语言标识符并将其硬编码到 ViewModel 中的每个 RelayCommand 中的语言创建一个 SetLanguageToXXX 命令.

Why do I want to do this? Part of "On The Fly Localization" In my particular implementation I want to create a SetLanguage RelayCommand that can be bound to multiple buttons. I would like to pass the two character language identifier ("en", "es", "ja", etc) as the CommandParameter and have that be defined for each "set language" button defined in the XAML. I want to avoid having to create a SetLanguageToXXX command for each language supporting and hard coding the two character language identifier into each RelayCommand in the ViewModel.

推荐答案

我不明白为什么你首先要指定 lambda 的额外复杂性.为什么不这样做:

I don't understand why you have the extra complexity of specifying the lambda in the first place. Why not just do this:

if (_aCommandWithAParameter == null)
{           
    _aCommandWithAParameter = new RelayCommand<object>(CommandWithAParameter);
}

private void CommandWithAParameter(object state)
{
    var str = state as string;
}

这篇关于使用 ViewModel 中定义的 RelayCommand 传递参数(来自 Josh Smith 示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆