Search code examples
linkerruntime-errormvvmcross

Binding MvvmCross TextColor not work for Android


I get the following warning:

  • [WARN] (MvxBind) Failed to create target binding for binding TextColor for MvxValueConverterValueCombiner combiner-operation

Binding code:

<TextView 
            android:id="@+id/text_view_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            style="@style/text_group_header"
            app:MvxBind="Text DisplayHeader; TextColor AppointmentFeedHeaderGroupColor(StartTime)"
        />

AppointmentFeedHeaderGroupColorValueConverter.cs

public class AppointmentFeedHeaderGroupColorValueConverter: MvxValueConverter<DateTime, Color>
    {
        private static readonly IDateTimeService DateTimeService = Mvx.Resolve<IDateTimeService>();
        private static readonly Color HeaderGroupBlack = GetColor(Resource.Color.black);
        private static readonly Color HeaderGroupRedLight = GetColor(Resource.Color.red_light);

        protected override Color Convert(DateTime value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != DateTimeService.Now.Date)
            {
                return HeaderGroupRedLight;
            }

            return HeaderGroupBlack;
        }

        private static Color GetColor(int resourceColorId)
        {
            var context = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;

            return new Color(ContextCompat.GetColor(context, resourceColorId));
        }
    }

I added the following code in file LinkerPleaseInclude.cs.

public void Include(TextView text)
{
    text.AfterTextChanged += (sender, args) => text.Text = "" + text.Text;
    text.Hint = "" + text.Hint;

    text.SetTextColor(Color.Black);
    var tc = text.TextColors;
    text.SetTextColor(tc);
} 

It did not help


Solution

  • First of all, check that you have installed MvvmCross.Plugin.Color in both your Android project and your PCL/NetStandard project.

    Then your converter should be in your PCL/NetStandard project and inherit from MvxColorValueConverter<DateTime> so that your color does not depend on your platform:

    public class AppointmentFeedHeaderGroupColorValueConverter: MvxColorValueConverter<DateTime>
    {
        private static readonly IDateTimeService DateTimeService = Mvx.Resolve<IDateTimeService>();
        private static readonly MvxColor HeaderGroupBlack = new MvxColor(0,0,0);
        private static readonly MvxColor HeaderGroupRedLight = new MvxColor(255,0,0); // red_light, should see how to convert this to RGB values
    
        protected override MvxColor Convert(DateTime value, object parameter, CultureInfo culture)
        {
            if (value != DateTimeService.Now.Date)
            {
                return HeaderGroupRedLight;
            }
    
            return HeaderGroupBlack;
        }
    }
    

    Finally, use the color converter as you are currently doing:

    TextColor AppointmentFeedHeaderGroupColor(StartTime)

    More info:

    MvvmCross Color docs

    MvvmCross.Plugin.Color.MvxColorValueConverter.cs

    And here you have the full LinkerPleaseInclude file provided by MvvmCross.

    HIH