1
votes

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);          

        }


    }
}
1
Just to clarify your basically just asking how to get an event or command to fire when you click a label? Currently OnDateClickIOSForResource is not firing and you want it to.David
Read through your question a couple times. From the Sounds of it clicking your label IS makeing the datapicker show up. However if after selecting a date you click the label a second time it shows the date you previously selected and you would prefer it showed the current date instead?David

1 Answers

0
votes

UIDatepicker defaults to current date unless you change it. Since you instatiating your Datepicker at the class level it maintains the date. Id think one or both of the following would fix it.

if (customStackLayout != null)
            {   
              datePicker = new UIDatePicker();

or

if (customStackLayout != null)
            {   
              datePicker.Date = DateTime.Now;

Not sure on the second whats dataType Date is you'll have to check it and set it as appropriate but hopefiully you get my point.