2016-12-30 62 views
0

我有一個自定義的渲染器,它是放置在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堆棧自定義渲染:

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

     } 


    } 
} 
+0

只是爲了澄清你基本上只是問如何獲得一個事件或命令點擊標籤時觸發?目前OnDateClickIOSForResource沒有開火,你想要它。 – David

+0

通讀您的問題幾次。從它的聲音中,點擊你的標籤就會顯示數據符號。但是,如果在選擇日期後第二次點擊標籤,它會顯示您之前選擇的日期,而您更願意顯示當前日期呢? – David

回答

0

的UIDatePicker默認爲當前日期,除非你改變它。由於您在課堂級別安裝日期選擇器,因此它會保留日期。我認爲以下一種或兩種都會解決它。

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

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

不知道在第二什麼的dataType日期是你必須檢查它並將其設置爲適當的,但hopefiully你明白我的意思。