使用值转换器在MVVM交叉中分配选择器(可绘制)的正确方法是什么?



我有一个视图

<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" />

我拥有转换器的绑定属性:

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;
            }
        }
    }

在查看模型中,我正在设置

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

我正在设置DiscoverYourScent = true;


当前没有设置。...使用值转换器在MVVM Cross中分配选择器(可绘制(的正确方法是什么。

您的绑定表达式是:

Drawable DiscoverYourScent;Converter=ButtonState

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

它需要是,逗号。

Drawable DiscoverYourScent, Converter=ButtonState

另外,您也可以写:

Drawable ButtonState(DiscoverYourScent)

,如果其余的绑定正确,则应该看到您的ValueConverter现在被击中。我相信您想绑定到Background作为源而不是Drawable。后者似乎不是Button上的公共财产。

正如乞力曼在对OP的评论中指出的那样,您也可以尝试设置Selected属性的运气,而不要更改可绘制的属性。只需确保您使用的可绘制的状态正确即可。

相关内容

最新更新