自定义渲染中的事件提升

时间:2016-12-30 16:10:14

标签: xaml xamarin xamarin.ios xamarin.forms

我有一个自定义渲染器,它是一个置于StackLayout内的DatePicker控件。我有一个标签,它将调用CustomRenderer并显示日期选择器控件。当我点击标签时,CustomRenderer(OnElementChanged&&& OnElementPropertyChanged)事件中没有引发任何事件。当我们更改选择器事件触发的日期并将值设置为日期选择器和日期选择器关闭时。当我再次尝试打开日期选择器时,显示前一个设置日期。我想改成现在的日期。我没有重置日期的事件。

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 个答案:

答案 0 :(得分:0)

除非您更改,否则UIDatepicker默认为当前日期。由于您在类级别实例化Datepicker,因此会保留日期。我认为以下一个或两个都可以解决它。

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

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

不确定第二个数据类型日期是否你必须检查它并将其设置为适当但是希望你能得到我的观点。