gpt4 book ai didi

android - ImageButton 在 MVVMCross 中的可见性

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:48 26 4
gpt4 key购买 nike

我有以下 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>

最佳答案

我会使用 Visibility Plugin (从 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;
}
);
}
}

然后使用可见性转换器和反向可见性转换绑定(bind)到 bool 值,并将命令绑​​定到按钮上的点击事件,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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>
</LinearLayout>

关于android - ImageButton 在 MVVMCross 中的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36610972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com