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

474 views Asked by At

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.

2

There are 2 answers

0
Amen Jlili On BEST ANSWER

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.

0
Vignesh kumar On

We have fixed the issue’s with “System.ArgumentException has been thrown while using EventToCommand behavior in SfCalendar”. As per the implementation Monthcell is moved to EventArgs from View and it is deprecated in OnMonthCellLoaded event and use MonthCellLoadedEventArgs. Please find the Custom assemblies for this fix below.

Custom assemblies: http://www.syncfusion.com/downloads/support/directtrac/217023/ze/Assembly1814496033.zip

Please clear the NuGet cache before replacing the custom assemblies. Please find the link below, https://www.syncfusion.com/kb/6987/how-to-clear-nuget-cache

Assembly Version: 16.3.0.21 Installation Directions: Replace the files “Syncfusion.SfCalendar.XForms.dll, Syncfusion.SfCalendar.XForms.Android.dll, Syncfusion.SfCalendar.XForms.iOS.dll” under following folders. Before replacing the new assemblies please take backup of old assemblies. {Syncfusion Installed location} \Essential Studio\16.3.0.21\Xamarin\lib\pcl\Syncfusion.SfCalendar.XForms.dll {Syncfusion Installed location} \Essential Studio\16.3.0.21\Xamarin\lib\Android\Syncfusion.SfCalendar.XForms.dll {Syncfusion Installed location}\EssentialStudio\16.3.0.21\Xamarin\lib\Android\Syncfusion.SfCalendar.XForms.Android.dll {Syncfusion Installed location} \Essential Studio\16.3.0.21\Xamarin\lib\iOS\Syncfusion.SfCalendar.XForms.dll {Syncfusion Installed location}\EssentialStudio\16.3.0.21\Xamarin\lib\iOS\Syncfusion.SfCalendar.XForms.iOS.dll

Regards,

Vigneshkumar R