Where to set calendar delegate?

3 posts, 1 answers
  1. Ian
    Ian avatar
    26 posts
    Member since:
    Jul 2017

    Posted 18 Oct Link to this post

    I want to set a delegate on a calendar in an iOS custom renderer.

    I have tried this...

     

    public class CustomCalendarRenderer : CalendarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<RadCalendar> e)
        {
            base.OnElementChanged(e);
     
            var myCalendarDelegate = new MyCalendarDelegate();
     
            Control.Delegate = myCalendarDelegate;
        }
    }

     

    But it throws a null reference exception on the final line.

    Is there a better place to set it?

  2. Answer
    Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    482 posts

    Posted 18 Oct Link to this post

    Hi Ian,

    You do this in the CreateCalendarDelegateOverride. You can see a full example of this in the SDKBrowser example app.

    Find the SDKBrowser app at the following location: C:\Program Files (x86)\Progress\Telerik UI for Xamarin R3 2017\Examples\XamarinForms

    Then drill down in the iOS project to the Calendar > Features > CustomRenderer folder. In there you'll find the CustomCalendarDelegate.cs and CustomCalendarRenderer.cs classes.

    For your convenience, I'll paste them below:

    [assembly: ExportRenderer(typeof(CustomCalendar), typeof(CustomCalendarRenderer))]
    namespace SDKBrowser.iOS.Calendar.Styling.CustomRenderer
    {
        public class CustomCalendarRenderer : CalendarRenderer
        {
            protected override CalendarDelegate CreateCalendarDelegateOverride()
            {
                return new CustomCalendarDelegate();
            }
        }
    }


    namespace SDKBrowser.iOS.Calendar.Styling.CustomRenderer
    {
        public class CustomCalendarDelegate : CalendarDelegate
        {
            public override void UpdateVisualsForCell(TKCalendar calendar, TKCalendarCell cell)
            {
                var dayCell = cell as TKCalendarDayCell;
     
                if (dayCell != null)
                {
                    this.SetBordersWidth(dayCell, 0);
     
                    TKCalendarDayState currentMonthState = TKCalendarDayState.CurrentMonth;
                    if ((dayCell.State & currentMonthState) == currentMonthState)
                    {
                        dayCell.Style.BackgroundColor = Color.FromHex("#F8F8F8").ToUIColor();
                        dayCell.Style.TextColor = Color.FromHex("#000000").ToUIColor();
                    }
                    else
                    {
                        dayCell.Style.BackgroundColor = Color.FromHex("#E0E0E0").ToUIColor();
                        dayCell.Style.TextColor = Color.FromHex("#FFFFFF").ToUIColor();
                    }
     
                    TKCalendarDayState weekendState = TKCalendarDayState.Weekend;
                    if ((dayCell.State & weekendState) == weekendState)
                    {
                        if ((dayCell.State & currentMonthState) == currentMonthState)
                        {
                            dayCell.Style.BackgroundColor = Color.FromHex("#EEEEEE").ToUIColor();
                            dayCell.Style.TextColor = Color.FromHex("#999999").ToUIColor();
                        }
                        else
                        {
                            dayCell.Style.BackgroundColor = Color.FromHex("#D0D0D0").ToUIColor();
                            dayCell.Style.TextColor = Color.FromHex("#AAAAAA").ToUIColor();
                        }
                    }
     
                    TKCalendarDayState todayState = TKCalendarDayState.Today;
                    if ((dayCell.State & todayState) == todayState)
                    {
                        var borderColor = Color.FromHex("#00FF44");
     
                        dayCell.Style.ShapeFill = null;
     
                        this.SetBordersColor(dayCell, borderColor);
                        this.SetBordersWidth(dayCell, 2);
                    }
     
                    TKCalendarDayState selectedState = TKCalendarDayState.Selected;
                    if ((dayCell.State & selectedState) == selectedState)
                    {
                        var borderColor = Color.FromHex("#0044FF");
     
                        dayCell.Style.ShapeFill = null;
     
                        this.SetBordersColor(dayCell, borderColor);
                        this.SetBordersWidth(dayCell, 2);
                    }
                }
            }
     
            private void SetBordersWidth(TKCalendarDayCell cell, int width)
            {
                cell.Style.TopBorderWidth = width;
                cell.Style.LeftBorderWidth = width;
                cell.Style.RightBorderWidth = width;
                cell.Style.BottomBorderWidth = width;
            }
     
            private void SetBordersColor(TKCalendarDayCell cell, Color color)
            {
                var uiColor = color.ToUIColor();
     
                cell.Style.TopBorderColor = uiColor;
                cell.Style.LeftBorderColor = uiColor;
                cell.Style.RightBorderColor = uiColor;
                cell.Style.BottomBorderColor = uiColor;
            }
        }
    }


    If you have any further trouble, feel free to open a Support Ticket here and attach all the relevant code so that we can investigate further.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Ian
    Ian avatar
    26 posts
    Member since:
    Jul 2017

    Posted 18 Oct in reply to Lance | Tech Support Engineer, Sr. Link to this post

    Great, thanks. Wish I'd looked harder for that.
Back to Top