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

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

问题描述

我想通过使用RelayCommand将我应用程序的XAML(View)定义的参数传递给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"/>

ViewModel代码

ViewModel Code

  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。这似乎很明显,因为使用文字StringApple调用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".

但是,查找执行堆栈,我可以看到橙色,我在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.

有没有办法?

为什么要这样做? On On Fly Localization的一部分
在我的特定实现中,我想创建一个可以绑定到多个按钮的SetLanguage RelayCommand。我想传递两个字符语言标识符(en,es,ja等)作为CommandParameter,并为XAML中定义的每个set language按钮定义。我想避免为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天全站免登陆