Search code examples
xamlxamarin.formsprismsyncfusionsfcalendar

Syncfusion calender OnMonthCellLoaded custom event is passing a null to my command


Preface: Syncfusion provides a free Calender control called SfCalendar for Xamarin.Forms. This calender has an event called OnMonthCellLoaded. The problem with this event is that its eventargs is type MonthCell which does not inherit from System.EventArgs unfortunately. This is a problem because the eventargs of an event must inherit from System.EvenArgs in order for it to properly used by the Prism EventToCommand behavior.

Objective: I am trying to bind the OnMonthCellLoaded event using prism behaviors in order to set the data context of the MonthCell. I hope this is clear.

Current situation:

I have extended the SfCalendar calender like follow:

 public class sfCalendarExtended : Syncfusion.SfCalendar.XForms.SfCalendar
    {
        public event EventHandler<MonthCellEventArgs> OnMonthCellLoadedExtended;

        public sfCalendarExtended()
        {
            this.OnMonthCellLoaded += SfCalendarExtended_OnMonthCellLoaded;
        }

        private void SfCalendarExtended_OnMonthCellLoaded(object sender, MonthCell e)
        {
           if (this.OnMonthCellLoadedExtended != null)
            {
                if (e != null)
                {
                        Debug.Print(e.Date.ToLongDateString());
                        var eventArgs = new MonthCellEventArgs() { Value = new MonthCell(e.Date) };
                        this.OnMonthCellLoadedExtended(this, eventArgs);
                }
            }
        }

    }

    public class MonthCellEventArgs : System.EventArgs
    {
        public MonthCell Value { get; set; }

        public MonthCellEventArgs()
        {

        }
    }

This is my Xaml

<Controls:sfCalendarExtended x:Name="calendar">
            <Syncfusion:SfCalendar.MonthViewSettings>
                <Syncfusion:MonthViewSettings DateSelectionColor="#dddddd" CellTemplate="{StaticResource weathertemplate}"/>
            </Syncfusion:SfCalendar.MonthViewSettings>
            <Syncfusion:SfCalendar.Behaviors>                   
                <prismbehaviors:EventToCommandBehavior  EventName="OnMonthCellLoadedExtended" Command="{Binding BindMonthCellToDateCommand}"/>
            </Syncfusion:SfCalendar.Behaviors>
        </Controls:sfCalendarExtended>

Where controls is the alias for the namespace where the sfCalenderExtended class resides.

Now let's take a look at the Command implementation in my view model:

public DelegateCommand<MonthCellEventArgs> BindMonthCellToDateCommand { get; set; }
        public ViewModel()
        {
            BindMonthCellToDateCommand = new DelegateCommand<MonthCellEventArgs>(
                (MonthCellEventArgs obj) => 
                {
    // more code here

Now everything goes according to plan until I hit MonthCellEventArgs obj with the debugger and obj is always null.

Any help would be highly appreciated.


Solution

  • Alright, so I have emailed Syncfusion about this and they have addressed this issue by changing the args parameter of MonthCellLoaded event handler to inherit from System.EventArgs. More information in their online forum here.

    My solution above works if and only if I use Corcav behaviors (see link) instead of Prism behaviors.