This is a migrated thread and some comments may be shown as answers.

Where to set calendar delegate?

2 Answers 87 Views
Calendar - Xamarin.iOS
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 18 Oct 2017, 05:24 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 18 Oct 2017, 06:59 PM
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
0
Ian
Top achievements
Rank 1
answered on 18 Oct 2017, 11:26 PM
Great, thanks. Wish I'd looked harder for that.
Tags
Calendar - Xamarin.iOS
Asked by
Ian
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Ian
Top achievements
Rank 1
Share this question
or