【问题标题】:MvvmCross Bind command to <include> toolbarMvvmCross 将命令绑定到 <include> 工具栏
【发布时间】:2016-07-28 00:07:58
【问题描述】:

我想将命令绑定到我的工具栏项目。可能吗? 我试过了,但还是不行https://stackoverflow.com/a/21936542/6160208

Toolbar.axml

<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:local="http://schemas.android.com/apk/res-auto"
            android:background="@android:color/holo_blue_light"
            android:layout_width="match_parent"
            android:layout_height="85dp">

        <ImageButton
            android:src="@drawable/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searchImageButton"
            android:layout_marginLeft="290dp"
            local:MvxBind="Click DoSearchCommand"
            android:background="@android:color/holo_blue_light" />

MainView.axml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:local="http://schemas.android.com/apk/res-auto"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
          <include
              layout="@layout/toolbar" />
</LinearLayout>

MainViewModel.cs

    private MvxCommand _searchCommand;
    public System.Windows.Input.ICommand SearchCommand
    {
        get
        {
            _searchCommand = _searchCommand ?? new MvxCommand(DoSearchCommand);
            return _searchCommand;
        }
    }
    private void DoSearchCommand()
    {
        ShowViewModel<SearchViewModel>();
    }

【问题讨论】:

    标签: xamarin.android mvvmcross xamarin.forms


    【解决方案1】:

    您绑定到DoSearchCommand,但这就是方法。你应该绑定到SearchCommand

    local:MvxBind="Click SearchCommand"
    

    作为改进,您也可以使用 IMvxCommand 而不是 ICommand,并将 ShowViewModel 添加为 lambda。

    private MvxCommand _searchCommand;
        public IMvxCommand SearchCommand
        {
            get
            {
                _searchCommand = _searchCommand ?? new MvxCommand(() => ShowViewModel<SearchViewModel>());
                return _searchCommand;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-18
      • 2014-03-23
      • 2017-08-18
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2020-03-10
      相关资源
      最近更新 更多