What is the correct way to use a value converter to assign a selector(drawable) in mvvm cross

时间:2017-06-06 16:29:40

标签: xamarin xamarin.android mvvmcross

I have a View

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
         android:background="@drawable/sel_fragfinder_toolbar_selection_selected"
            local:MvxBind="Drawable DiscoverYourScent;Converter=ButtonState"
            android:layout_weight="1"
            android:textColor="@android:color/white"
            android:textAllCaps="false"
            android:layout_marginRight="10dp"
            android:padding="10dp"
            android:text="Discover your scent" />

Where I have bind property for converter:

public class ButtonStateValueConverter : MvxValueConverter<bool,int>
    {
        protected override int Convert(bool value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value) {
              return  Resource.Drawable.sel_fragfinder_toolbar_selection_selected;
            }else{
              return  Resource.Drawable.sel_fragfinder_toolbar_selection_not_selected;
            }
        }
    }

In View Model, I am setting

private bool _discoverYourScent;
        public bool DiscoverYourScent
        {
            get { return _discoverYourScent; }
            set { _discoverYourScent = value; RaisePropertyChanged(() => DiscoverYourScent); }
        }

And I am setting DiscoverYourScent = true;


Currently It is not setting .... What is the correct way to use a value converter to assign a selector(drawable) in mvvm cross

1 个答案:

答案 0 :(得分:0)

您的绑定表达式是:

Drawable DiscoverYourScent;Converter=ButtonState

这里的问题是您的源目标分配和转换器语句之间有;分号。

它必须是,逗号。

Drawable DiscoverYourScent, Converter=ButtonState

或者你也可以写:

Drawable ButtonState(DiscoverYourScent)

如果绑定的其余部分正确,您应该立即看到ValueConverter被点击。我相信您希望绑定到Background作为源而不是Drawable。后者似乎不是Button上的公共财产。

正如Kiliman在对OP的评论中指出的那样,你也可以试试运气设置Selected属性,而不是改变你的Drawable。只需确保您使用的Drawable具有正确的状态。