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 |1 Answer
Reset to default 0You 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
DatePicker
at runtime? – Felix Shen Commented Mar 11 at 6:00