I have a custom Renderer which is a DatePicker Control placed inside a StackLayout. I have a label which will invoke the CustomRenderer and shows up date picker control. When i tap on the Label there is no event raised in the CustomRenderer (OnElementChanged && OnElementPropertyChanged) events. When we change the date the picker event fires and the value gets set to date picker and datepicker closes. When I try to open the date picker again the previous set date is showing. which i want to change to current date. i dont have the event to reset the date.
OnPlatform x:TypeArguments="View">
<OnPlatform.Android>
<local:CustomDatePicker AbsoluteLayout.LayoutBounds="1,0.5,-1,-1" WidthRequest="140" TextColor="Black" Date="{Binding ResourceDate}"
AbsoluteLayout.LayoutFlags="PositionProportional"/>
</OnPlatform.Android>
<OnPlatform.iOS>
<Label Text="{Binding ResourceDateIOS}" AbsoluteLayout.LayoutBounds="1,0.5,-1,-1" AbsoluteLayout.LayoutFlags="PositionProportional" FontSize="Small" TextColor="Black" >
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OnDateClickIOSForResource}" />
</Label.GestureRecognizers>
</Label>
</OnPlatform.iOS>
</OnPlatform>
IOS Stack Custom Render:
using Xamarin.Forms;
using UIKit;
using CoreGraphics;
using Xamarin.Forms.Platform.iOS;
using System;
using VCS.FieldManager.UI.ViewModels;
[assembly: ExportRenderer(typeof(CustomStackLayout), typeof(DatePickerExtendedRender))]
namespace App.UI.iOS
{
class DatePickerExtendedRender : ViewRenderer<CustomStackLayout, UIView>
{
UIDatePicker datePicker = new UIDatePicker();
protected override void OnElementChanged(ElementChangedEventArgs<CustomStackLayout> e)
{
if (e.OldElement != null || Element == null)
{
return;
}
var customStackLayout = e.NewElement;
if (customStackLayout != null)
{
datePicker.Mode = UIDatePickerMode.Date;
UIView _view = new UIView()
{
Frame = new CGRect(10, 200, 50, 50),
// BackgroundColor = UIColor.Brown
};
datePicker.ValueChanged += (sender, arg) =>
{
var DateChangeEvent = DateTime.SpecifyKind(datePicker.Date.ToDateTime(), DateTimeKind.Utc).ToLocalTime();
var changedDate = DateChangeEvent.ToString("MMMM dd, yyyy");
//shouldnot set in application variable for Add Resources
if (GridEntryViewModel.IsResourceDate)
{
Xamarin.Forms.Application.Current.Properties["ResourceSelectedDate"] = DateChangeEvent.ToString("MMMM dd, yyyy");
datePicker.SetDate(datePicker.Date, true);
}
else
{
Xamarin.Forms.Application.Current.Properties["GlobalDateSelected"] = changedDate;
GridEntryViewModel.IsResourceDate = false;
datePicker.SetDate(datePicker.Date, true);
}
MessagingCenter.Send(changedDate, "DatePickerCallback");
};
_view.Add(datePicker);
SetNativeControl(_view);
}
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
}
}
}