MVVMCross中ImageButton的可见性

cas*_*las 3 android mvvmcross xamarin

我有以下axml两个图像按钮,但我只想一次显示一个并实现切换功能.

更具体地说,当用户点击时button1,它将隐藏button1并显示button2,反之亦然.我正在使用MVVMCross模式.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">
    <ImageButton
        android:id="@+id/myBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_arrow_back" />
    <ImageButton
        android:id="@+id/myBtn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_arrow_white" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Iai*_*ith 7

我会使用Visibility插件(从NuGet添加它)

然后添加:bool属性和在ViewModel中切换bool的命令:

    private bool _boolInViewModel; 
    public bool BoolInViewModel
    {
        get { return _boolInViewModel; }
        set { _boolInViewModel = value; RaisePropertyChanged(() => BoolInViewModel);}
    }

    public IMvxCommand CommandToSwitchBool
    {
        get
        {
            return new MvxCommand(()=>
                {
                    BoolInViewModel = !BoolInViewModel;
                }
            );
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后使用可见性转换器和反向可见性转换以及命令到按钮上的click事件绑定到bool,如下所示:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">
    <ImageButton
        android:id="@+id/myBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_arrow_back"
        app:MvxBind="Visibility Visibility(BoolInViewModel); Click CommandToSwitchBool"/>
    <ImageButton
        android:id="@+id/myBtn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_arrow_white"
        app:MvxBind="Visibility InvertedVisibility(BoolInViewModel); Click CommandToSwitchBool"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)