1

I'm trying to access user-defined listeners in this Android library with Xamarin bindings (original here) for when the calendar is scrolled to another month or a date is selected on the calendar.

The listeners in the code given in the sample are as follows:

        compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
        @Override
        public void onDayClick(Date dateClicked) {
            List<Event> events = compactCalendarView.getEvents(dateClicked);
            Log.d(TAG, "Day was clicked: " + dateClicked + " with events " + events);
        }

        @Override
        public void onMonthScroll(Date firstDayOfNewMonth) {
            Log.d(TAG, "Month was scrolled to: " + firstDayOfNewMonth);
        }
    });

Specifically what I'd like to access is the onDayClick listener. The code for it in its Controller class in Java doesn't specify a button but rather calculates the position in the calendar of the date you clicked and then returns a date based on that calculation.

    void onSingleTapUp(MotionEvent e) {

    // Don't handle single tap when calendar is scrolling and is not stationary

    if (isScrolling()) {

        return;

    }



    int dayColumn = Math.round((paddingLeft + e.getX() - paddingWidth - paddingRight) / widthPerDay);

    int dayRow = Math.round((e.getY() - paddingHeight) / heightPerDay);



    setCalenderToFirstDayOfMonth(calendarWithFirstDayOfMonth, currentDate, monthsScrolledSoFar(), 0);



    int firstDayOfMonth = getDayOfWeek(calendarWithFirstDayOfMonth);



    int dayOfMonth = ((dayRow - 1) * 7) - firstDayOfMonth;

    if (isRtl) {

        dayOfMonth +=  6 - dayColumn;

    } else {

        dayOfMonth += dayColumn;

    }

    if (dayOfMonth < calendarWithFirstDayOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH)

            && dayOfMonth >= 0) {

        calendarWithFirstDayOfMonth.add(Calendar.DATE, dayOfMonth);



        currentCalender.setTimeInMillis(calendarWithFirstDayOfMonth.getTimeInMillis());

        performOnDayClickCallback(currentCalender.getTime());

    }

}

Attempting to declare a listener as CompactCalendarView.ICompactCalendarViewListener listener; seems to declare it fine, but attempting to assign it a new CompactCalendarViewListener() gives me an "Undefined function" error.

I understand C# uses events instead of listeners, but I don't know how it handles user-defined listeners in Java libraries, or how to override events such as onDayClick/onMonthScroll.

Any help is greatly appreciated!

4

1 回答 1

2

您在 Xamarin.Android 中使用 C# 的类不能是匿名的,因为 C# 不支持匿名声明,您需要做的是这样的:

compactCalendarView.SetListener(new CompactCalendarViewListener());

然后定义一个继承自Java.Lang.ObjectAndroid 的 Dispose 方法的类和您要使用的接口,如下所示:

 public class CompactCalendarViewListener : Java.Lang.Object, CompactCalendarView.ICompactCalendarViewListener
{
    public void OnDayClick(Date p0)
    {
        // throw new System.NotImplementedException();
    }

    public void OnMonthScroll(Date p0)
    {
        //throw new System.NotImplementedException();
    }
}
于 2019-02-28T15:01:26.593 回答