如何在.net maui中使用时间选择器微调器模式

6l7fqoea  于 2023-05-23  发布在  .NET
关注(0)|答案(1)|浏览(251)

我想在.net maui中使用时间选择器控件的微调器模式,但在我的应用程序中使用此控件显示时钟模式时,请帮助我更改时间选择器Currently displaying this model的模式。
Customize Android TimePicker to use Spinner in .net Maui我使用这个解决方案,但它是不工作,我认为这是不正确的解决方案.

qnzebej0

qnzebej01#

根据这个关于TimePickerDialog in spinner mode in the native android的案例,您可以将已弃用的Theme_Holo_Light_Dialog_NoActionBar主题传递给TimePickerDialog的构造函数。
您可以使用自定义处理程序来完成此操作。首先请在**/Platforms/Android**中创建Custom Handler类:

public class MyTimePickerHandler : TimePickerHandler
    {
        protected override TimePickerDialog CreateTimePickerDialog(int hour, int minute)
        {
            TimePickerDialog timePickerDialog =  new(
                              this.Context,
                         global::Android.Resource.Style.ThemeHoloLightDialogNoActionBar,
                         (s, e) => {
                                    this.VirtualView.Time = new TimeOnly(e.HourOfDay, e.Minute,0).ToTimeSpan();
                                    DateTime dateTime = DateTime.Today.Add(this.VirtualView.Time);
                                    this.PlatformView.Text = dateTime.ToString("h:mm tt"); ;
                              },
                              this.VirtualView.Time.Hours,
                              this.VirtualView.Time.Minutes,
                              false);
           
            return timePickerDialog;
        }
    }

然后使用mauiprogram.cs中的自定义处理程序:

builder
                  .UseMauiApp<App>()
                  .ConfigureFonts(fonts =>
                  {
                        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                        fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                  })
                  .ConfigureMauiHandlers(handlers =>
                  {
#if ANDROID
                        handlers.AddHandler(typeof(TimePicker), typeof(Platforms.Android.MyTimePickerHandler));
#endif
                  }) ;

和结果图像:

相关问题