有没有办法在 Xamarin Forms 中自定义日期选择器对话框的主体?
Is there a way to customise the body of a date picker's dialog in Xamarin Forms?
我正在为跨平台 Xamarin Forms 应用程序制作主题。我已经到了需要根据 light/dark 模式设置日期选择器样式的地步。选择器本身和它显示的文本都可以,具体取决于模式,但是对话框弹出窗口似乎有白色文本和白色背景,这使得它有点无用。有没有办法让我自定义它?
Image of a picker. I want to customise the white part
试试这个,在 Android 和 iOS
中添加了一些随机颜色
在styles.xml
<item name="android:datePickerDialogTheme">@style/CustomDatePickerDialog</item>
</style>
<style name="CustomDatePickerDialog" parent="ThemeOverlay.AppCompat.Dialog">
<!--header background-->
<item name="colorAccent">#009933</item>
<!--header textcolor-->
<item name="android:textColorPrimaryInverse">#ff9900</item>
<!--body background-->
<item name="android:windowBackground">#000099</item>
<!--selected day-->
<item name="android:colorControlActivated">#ff8000</item>
<!--days of the month-->
<item name="android:textColorPrimary">#ffff00</item>
<!--days of the week-->
<item name="android:textColorSecondary">#ff0066</item>
<!--cancel&ok-->
<item name="android:textColor">#00ffff</item>
</style>
在iOS
中添加Class
using DatePickerStackO.iOS;
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
[assembly: ExportRenderer(typeof(Xamarin.Forms.DatePicker), typeof(DateTimeRenderer))]
namespace DatePickerStackO.iOS
{
public class DateTimeRenderer : DatePickerRenderer
{
private UIDatePicker _picker;
bool _disposed;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
_picker = new UIDatePicker { Mode = UIDatePickerMode.Date, TimeZone = new NSTimeZone("UTC") };
if (UIDevice.CurrentDevice.CheckSystemVersion(14, 0))
{
_picker.PreferredDatePickerStyle = UIKit.UIDatePickerStyle.Wheels;
}
_picker.BackgroundColor = Color.Red.ToUIColor(); // YOUR COLOR HERE
_picker.ValueChanged -= HandleValueChanged;
_picker.ValueChanged += HandleValueChanged;
Control.InputView = _picker;
}
}
void HandleValueChanged(object sender, EventArgs e)
{
if (Element != null && Element.OnThisPlatform().UpdateMode() == UpdateMode.Immediately)
{
UpdateElementDate();
}
}
void UpdateElementDate()
{
Element?.SetValueFromRenderer(Xamarin.Forms.DatePicker.DateProperty, _picker.Date.ToDateTime().Date);
}
protected override void Dispose(bool disposing)
{
if (_disposed)
return;
_disposed = true;
if (disposing)
{
if (_picker != null)
{
_picker.RemoveFromSuperview();
_picker.ValueChanged -= HandleValueChanged;
_picker.Dispose();
_picker = null;
}
}
base.Dispose(disposing);
}
}
}
我使用了一个名为 XamForms.Controls.Calendar 的 nuget 包,它提供了一个易于定制的日历:
screenshot of package
这里有一个关于如何 implement
的小指南
希望这对您来说足够好:)
我正在为跨平台 Xamarin Forms 应用程序制作主题。我已经到了需要根据 light/dark 模式设置日期选择器样式的地步。选择器本身和它显示的文本都可以,具体取决于模式,但是对话框弹出窗口似乎有白色文本和白色背景,这使得它有点无用。有没有办法让我自定义它?
Image of a picker. I want to customise the white part
试试这个,在 Android 和 iOS
中添加了一些随机颜色在styles.xml
<item name="android:datePickerDialogTheme">@style/CustomDatePickerDialog</item>
</style>
<style name="CustomDatePickerDialog" parent="ThemeOverlay.AppCompat.Dialog">
<!--header background-->
<item name="colorAccent">#009933</item>
<!--header textcolor-->
<item name="android:textColorPrimaryInverse">#ff9900</item>
<!--body background-->
<item name="android:windowBackground">#000099</item>
<!--selected day-->
<item name="android:colorControlActivated">#ff8000</item>
<!--days of the month-->
<item name="android:textColorPrimary">#ffff00</item>
<!--days of the week-->
<item name="android:textColorSecondary">#ff0066</item>
<!--cancel&ok-->
<item name="android:textColor">#00ffff</item>
</style>
在iOS
中添加Classusing DatePickerStackO.iOS;
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
[assembly: ExportRenderer(typeof(Xamarin.Forms.DatePicker), typeof(DateTimeRenderer))]
namespace DatePickerStackO.iOS
{
public class DateTimeRenderer : DatePickerRenderer
{
private UIDatePicker _picker;
bool _disposed;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
_picker = new UIDatePicker { Mode = UIDatePickerMode.Date, TimeZone = new NSTimeZone("UTC") };
if (UIDevice.CurrentDevice.CheckSystemVersion(14, 0))
{
_picker.PreferredDatePickerStyle = UIKit.UIDatePickerStyle.Wheels;
}
_picker.BackgroundColor = Color.Red.ToUIColor(); // YOUR COLOR HERE
_picker.ValueChanged -= HandleValueChanged;
_picker.ValueChanged += HandleValueChanged;
Control.InputView = _picker;
}
}
void HandleValueChanged(object sender, EventArgs e)
{
if (Element != null && Element.OnThisPlatform().UpdateMode() == UpdateMode.Immediately)
{
UpdateElementDate();
}
}
void UpdateElementDate()
{
Element?.SetValueFromRenderer(Xamarin.Forms.DatePicker.DateProperty, _picker.Date.ToDateTime().Date);
}
protected override void Dispose(bool disposing)
{
if (_disposed)
return;
_disposed = true;
if (disposing)
{
if (_picker != null)
{
_picker.RemoveFromSuperview();
_picker.ValueChanged -= HandleValueChanged;
_picker.Dispose();
_picker = null;
}
}
base.Dispose(disposing);
}
}
}
我使用了一个名为 XamForms.Controls.Calendar 的 nuget 包,它提供了一个易于定制的日历:
screenshot of package
这里有一个关于如何 implement
的小指南希望这对您来说足够好:)