如何绑定TextView的TextColor?

时间:2016-03-21 14:43:28

标签: android xamarin mvvmcross

我正在尝试绑定Android中TextView的文本颜色。这是我的(截断的)xaml:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:MvxBind=" TextColor CategoryTextColor(Category)"/>

其中CategoryTextColorValueConverter如下:

public class CategoryTextColorConverter : MvxValueConverter<ShowCategory, Color>
{
    protected override Color Convert (ShowCategory value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == ShowCategory.AllShows)
        {
            return Color.Blue;
        }
        return Color.Red;
    }
}

调用转换器并按预期返回颜色,但文本颜色在TextView上永远不会改变。我有一个类似的背景颜色绑定工作正常。我在这里In MvvmCross how do I do custom bind properties看到我可能需要创建自定义绑定,但我找不到MvxBaseAndroidTargetBinding。也许我需要从nuget安装一个单独的包?

4 个答案:

答案 0 :(得分:8)

您唯一需要做的就是安装MvvMCross Color Plugin,因为var yellowStatuses = new List<string> { "A", "B", "C" }; var currentStatus = (String)DataBinder.Eval(e.Row.DataItem, "STATUS"); if (yellowStatuses.Contains(currentStatus)) e.Row.BackColor = Color.Yellow; 附带了它。它不包含在Core中。您发布的解决方案有效。

请参阅:https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#color

提示:您不必编写特定于平台的ValueConvertes,如果您使用TextColor,则可以在不同平台上共享它。

MvxColorValueConverter<ShowCategory>

答案 1 :(得分:2)

MVVMCross绑定通过绑定到对象的属性来工作,而android:textColor在Android XML中工作正常,它是对象的底层方法SetTextColor的快捷方式,你可以绑定它直接你可以做的是创建一个扩展TextView并具有可绑定TextColor属性的类,然后在Android XML中使用它并绑定到它。例如:

public class ExtendedTextView : TextView
{
    public ExtendedTextView(Context context): base (context) { }
    public ExtendedTextView(Context context, IAttributeSet attrs) : base (context, attrs) { }
    public ExtendedTextView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { }
    public ExtendedTextView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base (context, attrs, defStyleAttr, defStyleRes) { }
    protected ExtendedTextView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }

    public Color BindableTextColor // property to bind to in XML
    {
        get { return new Color(CurrentTextColor); }
        set { SetTextColor(value);}
    }
}

答案 2 :(得分:1)

感谢所有的输入 - 理想情况下我会使用@ sven-michael的跨平台解决方案,但我最终在Android中实现了自定义绑定。

我的代码如下。请注意,MvxBaseAndroidTargetBinding现在称为MvxAndroidTargetBinding

public class TextColorBinding : MvxAndroidTargetBinding {

    private readonly TextView _textView;

    public TextColorBinding(TextView textView) : base(textView)
    {

        _textView = textView;
    }

    public static void Register(IMvxTargetBindingFactoryRegistry registry) {
        registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("TextColor", (textView) => new TextColorBinding(textView)));
    }

    #region implemented abstract members of MvxTargetBinding

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

    #endregion

    #region implemented abstract members of MvxConvertingTargetBinding

    protected override void SetValueImpl (object target, object value)
    {
        var color = (Color)value;

        if (color != null) {

            var textView = (TextView)target;

            try {
                textView.SetTextColor (color);
            } catch (Exception ex) {
                MvxTrace.Error (ex.ToLongString ());
                throw;
            }

        } else {
            MvxBindingTrace.Trace (MvxTraceLevel.Warning, "Value was not a valid Color");
        }
    }
    #endregion
} 

不要忘记在Setup.cs中注册绑定,如下所示:

protected override void FillTargetFactories(MvvmCross.Binding.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
    {
        TextColorBinding.Register (registry);
        base.FillTargetFactories(registry);
    }

答案 3 :(得分:0)

为此,颜色调整和其他用途可以使用以下代码示例:

Android.Graphics.Color color = new Android.Graphics.Color(element.CurrentTextColor);