2016-06-09 55 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 
     });