11#nullable disable
22using System ;
3+ using System . Threading . Tasks ;
4+ using Microsoft . Maui . ApplicationModel ;
35using Microsoft . Maui . Controls . Internals ;
46using Microsoft . Maui . Graphics ;
57
@@ -15,7 +17,7 @@ public partial class DatePicker : View, IFontElement, ITextElement, IElementConf
1517 public static readonly BindableProperty DateProperty = BindableProperty . Create ( nameof ( Date ) , typeof ( DateTime ) , typeof ( DatePicker ) , default ( DateTime ) , BindingMode . TwoWay ,
1618 coerceValue : CoerceDate ,
1719 propertyChanged : DatePropertyChanged ,
18- defaultValueCreator : ( bindable ) => DateTime . Today ) ;
20+ defaultValueCreator : ( bindable ) => SetDefaultDate ( bindable ) ) ;
1921
2022 /// <summary>Bindable property for <see cref="MinimumDate"/>.</summary>
2123 public static readonly BindableProperty MinimumDateProperty = BindableProperty . Create ( nameof ( MinimumDate ) , typeof ( DateTime ) , typeof ( DatePicker ) , new DateTime ( 1900 , 1 , 1 ) ,
@@ -49,6 +51,22 @@ public partial class DatePicker : View, IFontElement, ITextElement, IElementConf
4951 public DatePicker ( )
5052 {
5153 _platformConfigurationRegistry = new Lazy < PlatformConfigurationRegistry < DatePicker > > ( ( ) => new PlatformConfigurationRegistry < DatePicker > ( this ) ) ;
54+ InitializeDateTimeNowAndSetDate ( ) ;
55+ }
56+
57+ //TODO: if no better solution is found, move this DateTime initialization somewhere else
58+ /// <summary>
59+ /// Initialize <see cref="DateTime"/> on startup since first call without initializing beforehand takes too much time
60+ /// See: https://github.com/dotnet/maui/issues/24929
61+ /// </summary>
62+ static DatePicker ( )
63+ {
64+ Task . Run ( ( ) =>
65+ {
66+ DateTime dateNow = DateTime . Now ;
67+ string dateString = dateNow . ToString ( ) ;
68+ _isDateTimeInitialized = true ;
69+ } ) ;
5270 }
5371
5472 /// <include file="../../docs/Microsoft.Maui.Controls/DatePicker.xml" path="//Member[@MemberName='Date']/Docs/*" />
@@ -239,5 +257,41 @@ string IDatePicker.Format
239257 get => Format ;
240258 set => SetValue ( FormatProperty , value , SetterSpecificity . FromHandler ) ;
241259 }
260+
261+ static bool _isDateTimeInitialized = false ;
262+ static DateTime SetDefaultDate ( BindableObject bindable )
263+ {
264+ if ( _isDateTimeInitialized )
265+ {
266+ return DateTime . Now ;
267+ }
268+ return ( DateTime ) MinimumDateProperty . DefaultValue ;
269+ }
270+
271+ /// <summary>
272+ /// If <see cref="DateTime.Now"/> hasn't been initialized, initialize it on worker thread to avoid slowing down first render
273+ /// See: https://github.com/dotnet/maui/issues/24929
274+ /// </summary>
275+ void InitializeDateTimeNowAndSetDate ( )
276+ {
277+ if ( _isDateTimeInitialized )
278+ {
279+ return ;
280+ }
281+ Task . Run ( ( ) =>
282+ {
283+ DateTime now = DateTime . Now ;
284+ _isDateTimeInitialized = true ;
285+ //Date might have been changed by client before initialization had a chance to finish
286+ if ( Date != MinimumDate )
287+ {
288+ return ;
289+ }
290+ MainThread . BeginInvokeOnMainThread ( ( ) =>
291+ {
292+ Date = now ;
293+ } ) ;
294+ } ) ;
295+ }
242296 }
243297}
0 commit comments