MvvmCross 在 Android 中将 bool 转换为 drawable

MvvmCross convert bool to drawable in Android

在我的 Xamarin Android 应用程序中,我尝试在布尔值上使用转换器来突出显示列表中的 selected 项。

有谁知道是否可以在布尔值上使用转换器 select 一个或另一个可绘制对象作为 LinearLayout 的背景?

我觉得我错过了什么。我试过从我的转换器返回各种类型,但似乎没有任何效果。

 <LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    local:MvxBind="Background IsSelected, Converter=BoolToDrawable"
    android:layout_height="wrap_content">

您可以像这个示例一样使用 BackgroundColor https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ValueConversion/ValueConversion.UI.Droid/Resources/Layout/View_Colors.axml#L40

我做过类似的事情,我有一个 int (RowItem.SummaryEnumPlayersInt),我将其转换为 drawable:

<TextView
    style="@style/TeamDifficulty"
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:gravity="center"
    android:background="@drawable/background_circle"
    local:MvxBind="Text RowItem.SummaryEnumPlayersInt; Summary RowItem.SummaryEnumPlayers" />

注意自定义 "Summary" 绑定,这就是魔术发生的地方,在设置中:

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
    base.FillTargetFactories(registry);

    registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("Summary", textView => new SummaryTextViewBinding(textView)));
}

这是自定义绑定 class:

public class SummaryTextViewBinding : MvxAndroidTargetBinding
{
    readonly TextView _textView;
    SummaryEnumeration _currentValue;

    public SummaryTextViewBinding(TextView textView) : base(textView)
    {
        _textView = textView;
    }

    #region implemented abstract members of MvxConvertingTargetBinding
    protected override void SetValueImpl(object target, object value)
    {            
        if (!string.IsNullOrEmpty(_textView.Text))
        {
            _currentValue = (SummaryEnumeration)Convert.ToInt32(_textView.Text);

            SetTextViewBackground();
        }
    }
    #endregion

    void SetTextViewBackground()
    {
        switch (_currentValue)
        {
            case SummaryEnumeration.Easy:
                _textView.SetBackgroundResource(Resource.Drawable.background_circle_green);
                _textView.Text = string.Empty;

                break;
            case SummaryEnumeration.Medium:
                _textView.SetBackgroundResource(Resource.Drawable.background_circle_yellow);
                _textView.Text = string.Empty;

                break;
            case SummaryEnumeration.Difficult:
                _textView.SetBackgroundResource(Resource.Drawable.background_circle_red);
                _textView.Text = string.Empty;

                break;
            case SummaryEnumeration.None:
                _textView.SetBackgroundResource(Resource.Drawable.background_circle_none);
                _textView.Text = LocalizationConstants.Nothing;

                break;
        }
    }

    public override Type TargetType
    {
        get { return typeof(bool); }
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }
}

尽情享受吧!