如何更改 Xamarin 表单中的 DatePicker Ok 和 Cancel 按钮文本?
Posted
技术标签:
【中文标题】如何更改 Xamarin 表单中的 DatePicker Ok 和 Cancel 按钮文本?【英文标题】:How to change DatePicker Ok and Cancel button text in Xamarin forms? 【发布时间】:2021-11-01 15:58:46 【问题描述】:我有一个支持多种语言的 Xamarin.Forms 应用程序。如何在资源文件中以本地语言文本显示 DatePicker 的日历和 DatePicker Ok 和 Cancel 按钮文本?
我的自定义渲染器
[assembly: ExportRenderer(typeof(CustomImageDatePicker), typeof(CustomImageDatePickerRenderer))]
namespace AMS.Droid.Renderers
public class CustomImageDatePickerRenderer : DatePickerRenderer
public CustomImageDatePickerRenderer(Context context) : base(context)
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
try
base.OnElementChanged(e);
CustomImageDatePicker element = (CustomImageDatePicker)this.Element;
if (Control == null)
return;
Control.Background = null;
if (Control != null && this.Element != null && !string.IsNullOrEmpty(element.Image))
Control.Background = AddPickerStyles(element.Image);
catch (Exception ex)
var message = ex.Message;
public LayerDrawable AddPickerStyles(string imagePath)
GradientDrawable gd = new GradientDrawable();
gd.SetColor(android.Graphics.Color.Transparent);
gd.SetCornerRadius(25);
gd.SetStroke(3, Android.Graphics.Color.Black);
this.Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
this.Control.SetPadding(20, 10, -50, 10);
Drawable[] layers = gd, GetDrawable(imagePath) ;
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.SetLayerInset(1, 0, 0, 30, 0);
return layerDrawable;
private BitmapDrawable GetDrawable(string imagePath)
try
int resID = Resources.GetIdentifier(imagePath.ToLower(), "drawable", this.Context.PackageName);
var drawable = ContextCompat.GetDrawable(this.Context, Resource.Drawable.brandIcon);
drawable.SetBounds(0, 0, (int)(drawable.IntrinsicWidth * 0.5),
(int)(drawable.IntrinsicHeight * 0.5));
var bitmap = ((BitmapDrawable)drawable).Bitmap;
var result = new BitmapDrawable(Resources, Bitmap.CreateScaledBitmap(bitmap, 60, 60, true));
result.Gravity = Android.Views.GravityFlags.Right;
//result.SetBounds(10, 10, 50, 0);
return result;
catch(Exception ex)
var message = ex.Message;
return null;
`
Xaml:
<customDatePicker:CustomDatePicker
x:Name="dpFromDate"
DateSelected="FromDate_Selected"
Margin="10,5,10,0"
Image="brandIcon.png"/>
【问题讨论】:
你当前的 DatePicker 代码是什么? @FreakyAli 我已经用上面的代码更新了我的问题。 【参考方案1】:您可以使用自定义渲染器来做到这一点。
在 Android 上,可以通过覆盖自定义渲染器中的 CreateDatePickerDialog
方法来自定义 DatePicker 对话框。
[assembly: ExportRenderer(typeof(Xamarin.Forms.DatePicker), typeof(CustomDatePickerRenderer))]
namespace App10.Droid
public class CustomDatePickerRenderer : DatePickerRenderer
public CustomDatePickerRenderer(Context context) : base(context)
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
base.OnElementChanged(e);
//Disposing
if (e.OldElement != null)
datePicker = null;
//Creating
if (e.NewElement != null)
datePicker = e.NewElement;
DatePickerDialog pickerDialog;
private Xamarin.Forms.DatePicker datePicker;
protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
pickerDialog = new DatePickerDialog(Context, (o, e) =>
datePicker.Date = e.Date;
((IElementController)datePicker).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
, year, month, day);
//ok
pickerDialog.SetButton((int)DialogButtonType.Positive, "(FR)-DONE", OnDone);
//cancel
pickerDialog.SetButton((int)DialogButtonType.Negative, "(FR)-CLEAR", OnCancel);
return pickerDialog;
private void OnCancel(object sender, DialogClickEventArgs e)
datePicker.Unfocus();
private void OnDone(object sender, DialogClickEventArgs e)
datePicker.Date = ((DatePickerDialog)sender).DatePicker.DateTime;
datePicker.Unfocus();
您可以将 (FR)-DONE
和 (FR)-CLEAR
替换为资源文件中您自己的本地语言文本
//ok
pickerDialog.SetButton((int)DialogButtonType.Positive, "(FR)-DONE", OnDone);
//cancel
pickerDialog.SetButton((int)DialogButtonType.Negative, "(FR)-CLEAR", OnCancel);
【讨论】:
以上是关于如何更改 Xamarin 表单中的 DatePicker Ok 和 Cancel 按钮文本?的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin 表单 - Webview 检测 URL 更改
如何在 xamarin 表单中更改视觉材料 DatePickerDialog 和 TimePickerDialog 背景颜色