2016-04-13 18 views
2

私は2つの画像ボタンを持っているaxmlを持っていますが、一度に1つずつ表示し、何かトグル機能を実装したいだけです。MVVMCrossのImageButtonの可視性

具体的には、ユーザーが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> 

答えて

5

私が追加し(NuGetから追加)

Visibility Pluginを使用します。ブールプロパティおよびコマンドを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; 
       } 
      ); 
     } 
    } 

をし、その後にバインド可視性コンバータと逆視界変換を持つブールに変換し、ボタンのクリックイベントへのコマンドを次のようにします。

<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> 
+0

ありがとう、それは完璧に働いた。 InvertedVisibility機能を知っておいてよかった! – hotspring

+1

ええ、「リストのアイテムが表示されない」ビューを表示/非表示にするために、リスト上のカウントへのバインディングの可視性のように、すばらしいことがたくさんあります。クレジットは本当にここにプラグインを作っている人に行くhttps://github.com/MvvmCross/MvvmCross-Plugins/tree/master/Visibility –