android - How Do I change Dialog from DatePicker in MAUI from C#? - Stack Overflow

I can't find out how to change the Android native datepicker dialogs in .Net MAUI from C#.I know

I can't find out how to change the Android native datepicker dialogs in .Net MAUI from C#. I know i can change it by modifying the file colors.xml from Platforms/Android/Resources. But i Would like to change the color from C# because i want it to be a dynamic color, and it will be changing.

At the moment I have it in red color.

Any idea what can i do?

I can't find out how to change the Android native datepicker dialogs in .Net MAUI from C#. I know i can change it by modifying the file colors.xml from Platforms/Android/Resources. But i Would like to change the color from C# because i want it to be a dynamic color, and it will be changing.

At the moment I have it in red color.

Any idea what can i do?

Share Improve this question edited Mar 10 at 13:58 David Picazo asked Mar 10 at 10:29 David PicazoDavid Picazo 374 bronze badges 5
  • Do you mean changing the color of the DatePicker at runtime? – Felix Shen Commented Mar 11 at 6:00
  • yeah, I didn't expressed myself clearly enough sorry. – David Picazo Commented Mar 11 at 7:38
  • Do you want to change the color after the dialogue pops up or before it pops up? – Felix Shen Commented Mar 11 at 9:29
  • before it pops up. I want to link the color of the dialogue with my main application color. It gets the color from colors.xml and i want the color of my Colors.xaml file. – David Picazo Commented Mar 11 at 9:58
  • According to the Android DatePickerDialog docs, you may predefine the theme you want in android platform folder before you build the application. And when the app runs, you get the color and use the corresponding theme to style the dialogue. – Felix Shen Commented Mar 13 at 8:52
Add a comment  | 

1 Answer 1

Reset to default 0

You may try using Handler to customize the DatePicker in native way. I made a demo below.

Let's say we define a DatePicker in xaml,

<DatePicker MinimumDate="01/01/2022"
            MaximumDate="12/31/2025"
            Date="06/21/2022" />

According to the Android DatePickerDialog docs, if we want to set the color for DatePickerDialog, we may set a theme for it. So we may first create a new theme for it. For example, under the Platforms/Android/Resources/values folder, create a new theme file named DatePickerStyle1.xml. Then we may use this them to customize the DatePicker,

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
    <style name="DatePickerStyle1" parent="Theme.AppCompat.Light.Dialog">
        <!--header background-->
        <item name="colorAccent">#ff0000</item>         
        <!--cancel&ok-->
        <item name="android:textColor">#ff0000</item>
    </style>
</resources>

After that, in code behind, we use Handler to customize the DatePicker in a native way, like below,

        public MainPage()
        {
            InitializeComponent();
            ModifyDatePicker();
        }


        //Let's say here is a button to control which theme to use for DatePicker
        private void CounterBtn_Clicked(object sender, EventArgs e)
        {
             //we use Preference to store a value
             Preferences.Set("count",Preferences.Get("count",0) + 1);
        }

        void ModifyDatePicker()
        {
            Microsoft.Maui.Handlers.DatePickerHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
            {
#if ANDROID                               
                handler.PlatformView.SetOnClickListener(new MyClickListener());        
#endif
            });
        }
    }
#if ANDROID
    public class MyClickListener : Java.Lang.Object, IOnClickListener
    {
        MauiDatePicker md;
        public void OnClick(Android.Views.View? v)
        {
            md = v as MauiDatePicker;
            Console.WriteLine();
            DateTime today = DateTime.Today;
            

           // You can add code here to decide which style you want to use
            var style = Preferences.Get("count", 0) % 2 == 0 ? Resource.Style.DatePickerStyle1 : Resource.Style.DatePickerStyle2;

            // create a new dialog and use the theme we define before
            DatePickerDialog dialog = new DatePickerDialog(Platform.CurrentActivity,style  ,OnDataSet,today.Year,today.Month -1,today.Day);           
            dialog.Show();
        }

        private void OnDataSet(object? sender, DatePickerDialog.DateSetEventArgs e)
        {
            md.Text = new DateTime(e.Year,e.Month + 1,e.DayOfMonth).ToShortDateString();
        }
    }
#endif

You can get the color from XAML and choose the corresponding theme for the DatePicker in this way.

Hope it helps!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744851974a4597179.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信