Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Platform Effect Demo
public class DateRangeChangedEventArgs : System.EventArgs
{
public DateRangeChangedEventArgs(DateTime startDate, DateTime endDate)
{
StartDate = startDate;
EndDate = endDate;
}
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:telerikInput="clr-namespace:Telerik.XamarinForms.Input;assembly=Telerik.XamarinForms.Input"
xmlns:effects="clr-namespace:RangeSelectionTest.Portable.Effects;assembly=RangeSelectionTest.Portable"
x:Class="RangeSelectionTest.Portable.MainPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<telerikInput:RadCalendar x:Name="radCalendar">
<telerikInput:RadCalendar.Effects>
<effects:RangeSelectionEffect x:Name="SelectionEffect" />
</telerikInput:RadCalendar.Effects>
</telerikInput:RadCalendar>
<Button Text="Change Date Range"
Clicked="Button_OnClicked"
Grid.Row="1" />
</Grid>
</ContentPage>
using System;
using Xamarin.Forms;
[assembly:XamlCompilation(XamlCompilationOptions.Compile)]
namespace RangeSelectionTest.Portable
{
public partial class MainPage : ContentPage
{
private int incrementer = 1;
public MainPage()
{
InitializeComponent();
SelectionEffect.StartDate = DateTime.Today.AddDays(-3);
SelectionEffect.EndDate = DateTime.Today;
}
private void Button_OnClicked(object sender, EventArgs e)
{
incrementer++;
SelectionEffect.StartDate = DateTime.Today.AddDays(incrementer - 3);
SelectionEffect.EndDate = DateTime.Today.AddDays(incrementer);
}
}
}
using System;
using System.Linq;
using Com.Telerik.Widget.Calendar;
using Java.Util;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ResolutionGroupName("LancelotSoftware")]
[assembly: ExportEffect(typeof(RangeSelectionTest.Android.Effects.RangeSelectionEffect), "RangeSelectionEffect")]
namespace RangeSelectionTest.Android.Effects
{
public class RangeSelectionEffect : PlatformEffect
{
protected override void OnAttached()
{
if (Element.Effects.FirstOrDefault (e => e is Portable.Effects.RangeSelectionEffect) is Portable.Effects.RangeSelectionEffect effect)
{
effect.DateRangeValueChanged += Effect_DateRangeValueChanged;
if (Control is RadCalendarView calendar)
{
calendar.SelectionMode = CalendarSelectionMode.Range;
}
}
}
private void Effect_DateRangeValueChanged(object sender, Portable.Effects.DateRangeChangedEventArgs args)
{
if (Control is RadCalendarView calendarView)
{
calendarView.SelectedRange = new DateRange(ConvertToCalendar(args.StartDate).TimeInMillis, ConvertToCalendar(args.EndDate).TimeInMillis);
}
}
protected override void OnDetached()
{
if (Element.Effects.FirstOrDefault (e => e is Portable.Effects.RangeSelectionEffect) is Portable.Effects.RangeSelectionEffect effect)
{
effect.DateRangeValueChanged -= Effect_DateRangeValueChanged;
}
}
public static Calendar ConvertToCalendar(DateTime date)
{
Calendar calendar = Calendar.Instance;
calendar.Set(date.Year, date.Month - 1, date.Day, date.Hour, date.Minute, date.Second);
return calendar;
}
}
}
using System;
using Xamarin.Forms;
namespace RangeSelectionTest.Portable.Effects
{
public class RangeSelectionEffect : RoutingEffect
{
private DateTime endDate;
private DateTime startDate;
public RangeSelectionEffect()
: base("LancelotSoftware.RangeSelectionEffect")
{
}
public DateTime StartDate
{
get => startDate;
set
{
if (startDate == value)
return;
startDate = value;
ExecuteValueChanged();
}
}
public DateTime EndDate
{
get => endDate;
set
{
if (endDate == value)
return;
endDate = value;
ExecuteValueChanged();
}
}
public delegate void DateRangeChanged(object sender, DateRangeChangedEventArgs e);
public event DateRangeChanged DateRangeValueChanged;
private void ExecuteValueChanged()
{
DateRangeValueChanged?.Invoke(this, new DateRangeChangedEventArgs(this.StartDate, this.EndDate));
}
}
}
using System;
using System.Linq;
using Foundation;
using TelerikUI;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ResolutionGroupName("LancelotSoftware")]
[assembly: ExportEffect(typeof(RangeSelectionTest.iOS.Effects.RangeSelectionEffect), "RangeSelectionEffect")]
namespace RangeSelectionTest.iOS.Effects
{
public class RangeSelectionEffect : PlatformEffect
{
protected override void OnAttached()
{
if (Element.Effects.FirstOrDefault (e => e is Portable.Effects.RangeSelectionEffect) is Portable.Effects.RangeSelectionEffect effect)
{
effect.DateRangeValueChanged += Effect_DateRangeValueChanged;
if (Control is TKCalendar calendar)
{
calendar.SelectionMode = TKCalendarSelectionMode.Range;
}
}
}
private void Effect_DateRangeValueChanged(object sender, Portable.Effects.DateRangeChangedEventArgs args)
{
if (Control is TKCalendar calendar)
{
calendar.SelectedDatesRange = new TKDateRange
{
StartDate = ToNSDate(args.StartDate),
EndDate = ToNSDate(args.EndDate)
};
}
}
protected override void OnDetached()
{
if (Element.Effects.FirstOrDefault (e => e is Portable.Effects.RangeSelectionEffect) is Portable.Effects.RangeSelectionEffect effect)
{
effect.DateRangeValueChanged -= Effect_DateRangeValueChanged;
}
}
// ReSharper disable once InconsistentNaming
private static NSDate ToNSDate(DateTime date)
{
if (date.Kind == DateTimeKind.Unspecified)
date = DateTime.SpecifyKind(date, DateTimeKind.Local);
return (NSDate) date;
}
}
}
using System.Linq;
using RangeSelectionTest.UWP.Effects;
using Telerik.UI.Xaml.Controls.Input;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
[assembly: ResolutionGroupName("LancelotSoftware")]
[assembly: ExportEffect(typeof(RangeSelectionTest.UWP.Effects.RangeSelectionEffect), "RangeSelectionEffect")]
namespace RangeSelectionTest.UWP.Effects
{
public class RangeSelectionEffect: PlatformEffect
{
protected override void OnAttached()
{
if (Element.Effects.FirstOrDefault (e => e is Portable.Effects.RangeSelectionEffect) is Portable.Effects.RangeSelectionEffect effect)
{
effect.DateRangeValueChanged += Effect_DateRangeValueChanged;
if (Control is RadCalendar calendar)
{
calendar.SelectionMode = CalendarSelectionMode.Multiple;
}
}
}
private void Effect_DateRangeValueChanged(object sender, Portable.Effects.DateRangeChangedEventArgs args)
{
if (Control is RadCalendar calendar)
{
var dateRange = new CalendarDateRange
{
StartDate = args.StartDate,
EndDate = args.EndDate
};
calendar.SelectedDateRange = dateRange;
}
}
protected override void OnDetached()
{
if (Element.Effects.FirstOrDefault (e => e is Portable.Effects.RangeSelectionEffect) is Portable.Effects.RangeSelectionEffect effect)
{
effect.DateRangeValueChanged -= Effect_DateRangeValueChanged;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment