【问题标题】:Using MvxCommand With CommandParameter binding将 MvxCommand 与 CommandParameter 绑定一起使用
【发布时间】:2013-06-18 13:02:37
【问题描述】:

我正在尝试使用火MvxCommand with CommandParameter,但遇到以下问题: MyView.axml 包含:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        local:MvxBind="Click MyCommand, CommandParameter=foo" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        local:MvxBind="Click MyCommand, CommandParameter=bar" />
</LinearLayout>

MyViewModel.cs:

public class MyViewModel : MvxViewModel
{
    public ICommand MyCommand { get; private set; }

    public MyViewModel()
    {                                    // param is null
      MyCommand = new MvxCommand<string>(param =>
      {
          if (param == "foo")               
          {
            // do something
          }
          else if (param == "bar")
          {
            // do something else
          }
      });
    }
}

但是当我检查 param 变量是 null

我做错了什么?

【问题讨论】:

    标签: mvvm binding command xamarin.android mvvmcross


    【解决方案1】:

    您的代码在我的源代码树的头部为我工作。

    但这个功能只有两周的历史。

    我的猜测是,此功能要么没有进入您正在使用的版本,要么存在错误。

    你能检查你的调试跟踪这个绑定吗?有什么资料吗?

    • 如果跟踪表明 CommandParameter 是一个未知符号,那么我的猜测是您需要自己构建最新的源代码 - 或者等待新版本。
    • 如果跟踪提示其他问题,那么您可以在设置期间修补问题。

    我知道我们确实修复了一个值转换器问题,其中基于 ValueConverterCirrious.MvvmCross.Binding.dll 不仅仅是通过覆盖 Setup.ValueConverterAssemblies 来注册此 CommandParameter 所需的 ValueConverter

    【讨论】:

    • 你说得对,斯图尔特!需要查看日志:MvxBind:Warning:132,54 找不到命名转换器 CommandParameter I/MvxBind (18314):132,53 找不到命名转换器 CommandParameter I/mono-stdout(18314):MvxBind:Warning:132 ,53 找不到命名转换器命令参数 预计何时发布最新版本?
    • @Stuart 我正在尝试相同的local:MvxBind="Click OnRemoveClick, CommandParameter=." 我得到相同的“。”在执行命令时。请问有什么想法吗?
    • 对不起 - 听起来你和这个 2013 年的问题完全不同。可能最好在一个新问题中完整地解释您的问题?
    • @Stuart 如果我们想使用一个对象作为参数而不仅仅是一个字符串?
    【解决方案2】:

    我今天在做 CommandParameter 编码,你需要做几个修复。 axml 代码应该包含 CommandParameter='yourParameter' 它看起来像这样:

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        local:MvxBind="Click MyCommand, CommandParameter='foo'" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        local:MvxBind="Click MyCommand, CommandParameter='bar'" />
    

    即使你想捕捉一个整数,你仍然需要在单引号中传递这个:CommandParameter='1234'

    在 C# 代码中,最重要的是从构造函数中移除 MvxCommand。这应该被视为属性。

    public class MyViewModel : MvxViewModel
    {
        public MyViewModel() { }
    
        public MvxCommand<string> MyCommand
        {
            get
            {
                return new MvxCommand<string>(param => 
                {
                    if (param == "foo")
                    {
                        // do something
                    }
                    else if (param == "bar")
                    {
                        // do something else
                    }
                });
            }
        }
    }
    

    这是在 MvvmCross6 中完成的。它应该适用于以前的版本。

    【讨论】:

      猜你喜欢
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 2015-05-15
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多