@@ -8,8 +8,7 @@ namespace Microsoft.Maui.Handlers
88 public partial class DatePickerHandler : ViewHandler < IDatePicker , MauiDatePicker >
99 {
1010 DatePickerDialog ? _dialog ;
11- EventHandler ? _setDateLater ;
12- EventHandler ? _dismiss ;
11+ ( int year , int month , int day ) _selectedDate ;
1312
1413 protected override MauiDatePicker CreatePlatformView ( )
1514 {
@@ -54,8 +53,11 @@ protected override void DisconnectHandler(MauiDatePicker platformView)
5453 {
5554 if ( _dialog != null )
5655 {
57- _dialog . DismissEvent -= _dismiss ;
58- _dialog . ShowEvent -= _setDateLater ;
56+ _dialog . DismissEvent -= OnDismissEvent ;
57+ _dialog . ShowEvent -= OnShowEvent ;
58+ if ( OperatingSystem . IsAndroidVersionAtLeast ( 24 ) )
59+ _dialog . DateSet -= OnDateSet ;
60+
5961 _dialog . Hide ( ) ;
6062 _dialog . Dispose ( ) ;
6163 _dialog = null ;
@@ -70,17 +72,7 @@ protected override void DisconnectHandler(MauiDatePicker platformView)
7072
7173 protected virtual DatePickerDialog CreateDatePickerDialog ( int year , int month , int day )
7274 {
73- void OnDateSelectedCallback ( object ? obj , DatePickerDialog . DateSetEventArgs e )
74- {
75- if ( VirtualView != null )
76- {
77- VirtualView . Date = e . Date ;
78- VirtualView . IsFocused = false ;
79- }
80- }
81-
82- var dialog = new DatePickerDialog ( Context ! , OnDateSelectedCallback , year , month , day ) ;
83-
75+ var dialog = new DatePickerDialog ( Context ! , OnDateSet , year , month , day ) ;
8476 return dialog ;
8577 }
8678
@@ -148,22 +140,42 @@ void ShowPickerDialog(int year, int month, int day)
148140 _dialog = CreateDatePickerDialog ( year , month , day ) ;
149141 else
150142 {
151- _setDateLater = ( sender , e ) => { _dialog ! . UpdateDate ( year , month , day ) ; _dialog . ShowEvent -= _setDateLater ; } ;
152- _dialog . ShowEvent += _setDateLater ;
153- _dismiss = ( sender , e ) =>
154- {
155- if ( VirtualView != null )
156- {
157- VirtualView . IsFocused = false ;
158- }
159- _dialog . DismissEvent -= _dismiss ;
160- } ;
161- _dialog . DismissEvent += _dismiss ;
143+ _selectedDate = ( year , month , day ) ;
144+ _dialog . ShowEvent += OnShowEvent ;
145+ _dialog . DismissEvent += OnDismissEvent ;
162146 }
163147
164148 _dialog . Show ( ) ;
165149 }
166150
151+ void OnDismissEvent ( object ? sender , EventArgs e )
152+ {
153+ if ( VirtualView != null )
154+ {
155+ VirtualView . IsFocused = false ;
156+ }
157+
158+ if ( _dialog != null )
159+ {
160+ _dialog . DismissEvent -= OnDismissEvent ;
161+ }
162+ }
163+
164+ void OnShowEvent ( object ? sender , EventArgs e )
165+ {
166+ _dialog ! . UpdateDate ( _selectedDate . year , _selectedDate . month , _selectedDate . day ) ;
167+ _dialog . ShowEvent -= OnShowEvent ;
168+ }
169+
170+ void OnDateSet ( object ? obj , DatePickerDialog . DateSetEventArgs e )
171+ {
172+ if ( VirtualView != null )
173+ {
174+ VirtualView . Date = e . Date ;
175+ VirtualView . IsFocused = false ;
176+ }
177+ }
178+
167179 void HidePickerDialog ( )
168180 {
169181 _dialog ? . Hide ( ) ;
0 commit comments