使用Xamarin.Android中的MaterialCalendarView绑定库添加装饰

时间:2017-07-19 14:15:21

标签: android xamarin

我在我的Xamarin.Android应用程序中使用MaterialCalendarView库,除了我无法在日历中添加跨度装饰外,它在大多数情况下运行良好。

这是我的代码:

public class Calendar : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Calendar);

            MaterialCalendarView calendar = FindViewById<MaterialCalendarView>(Resource.Id.calendarView);
            calendar.SetSelectedDate(Java.Util.Calendar.GetInstance(Java.Util.Locale.English));

            List<CalendarDay> dates = new List<CalendarDay>();

            CalendarDay a = CalendarDay.From(2017, 7, 10);
            CalendarDay b = CalendarDay.From(2017, 7, 15);
            CalendarDay c = CalendarDay.From(2017, 7, 9);
            CalendarDay d = CalendarDay.From(2017, 6, 1);

            dates.Add(a);
            dates.Add(b);
            dates.Add(c);
            dates.Add(d);

            //calendar.Post(() =>
            //{
            //    calendar.AddDecorators(new EventDecorator(Android.Resource.Color.HoloOrangeLight, dates));
            //});

            calendar.AddDecorators(new EventDecorator(Android.Resource.Color.HoloOrangeLight, dates));

        }
    }

class EventDecorator : Java.Lang.Object, IDayViewDecorator
{
    private int color;
    private List<CalendarDay> dates;

    public EventDecorator(int color, List<CalendarDay> dates)
    {
        this.color = color;
        this.dates = dates;
    }

    public void Decorate(DayViewFacade view)
    {
        view.AddSpan(new DotSpan(5, color));
    }

    public bool ShouldDecorate(CalendarDay day)
    {
        //return dates.Contains(day);
        return dates.Exists(e => (e == day));
    }
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

  

我无法在日历中添加跨度装饰。

问题是,当您使用ShouldDecoratedates.Contains(day)时,dates.Exists(e => (e == day))始终返回false。

它将比较HashCode firet所以它总是返回false,修改你的代码就像下面这样可以解决这个问题:

if (dates[0].ToString() == day.ToString() || dates[1].ToString() == day.ToString() || dates[2].ToString() == day.ToString() || dates[3].ToString() == day.ToString())
{
      return true;
}
else
{
       return false;
}

这样的效果:

enter image description here