2016-06-09 123 views
0

我用我的Xamarin.Android項目中MVVM光想我MainViewModelStatusColor屬性綁定到GradientDrawable其充當ImageView控制的背景的背景光MVVM 。綁定Drawble背景顏色與Xamarin.Android

不幸的是,GradientDrawable沒有任何Color屬性,我可以綁定我的顏色,只是一個SetColor(int)方法。當我的MainViewModelStatusColor屬性發生變化時,是否有辦法告訴MVVM燈始終調用此方法SetColor(int)

醜陋的替代方法是當視圖模型改變了顏色屬性觸發事件,但我真的想避免...

我的代碼目前看起來是這樣的(!):

// This does not work! 
this.SetBinding(
    () => MainViewModel.StatusColor, 
    () => ((GradientDrawable)IvStatus.Background).Color); // There is no GradientDrawable.Color property... 

如果有幫助,這是我IvStatus控制和Drawable其背景設置爲:

IvStatus

<ImageView 
    android:id="@+id/ivStatus" 
    android:background="@drawable/Circle"          
    android:layout_width="10dip" 
    android:layout_height="10dip" 
    android:layout_gravity="center_vertical" /> 

Circle.xml

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <solid android:color="#cccccc"/> 
    <stroke 
     android:width="1dip" 
     android:color="#333333"/> 
</shape> 

提前感謝!

+0

我不認爲你可以。也許看看MvvmCross庫,他們可能有一個綁定解決方法。但據我所知MvvmLight你應該只是訂閱屬性更改事件,在你的Android應用程序,並手動創建一個新的GradientBrush當你的虛擬機中的顏色值改變 – Miiite

回答

1

使用Binding.WhenSourceChanges方法。

this.SetBinding(
    () => MainViewModel.StatusColor) 
    .WhenSourceChanges(() => 
     { 
      // This is where you can use SetColor 
     });