![](/img/trans.png)
[英]Xamarin.Forms - How do I change the background color of the screen at random times?
[英]How do I change the locale for the datepicker in Xamarin.Forms?
我有一個支持英語和法語的 Xamarin.Forms 應用程序。 如何在法語語言環境中顯示 DatePicker 的日歷(月份、星期幾文本)?
我試過 Xamarin.Forms
public void SetLocale(CultureInfo ci)
{
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
Console.WriteLine("CurrentCulture set: " + ci.Name);
}
在我的 DatePicker 自定義渲染器中創建日期選擇器對話框之前,我嘗試更改上下文
var config = new Android.Content.Res.Configuration { Locale = Locale.CanadaFrench };
Context.Resources.UpdateConfiguration(config, Context.Resources.DisplayMetrics);
_dialog = new DatePickerDialog(Context, (o, e) =>
{
view.Date = e.Date;
((IElementController)view).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
}, year, month, day);
我也在我的 DatePicker 自定義渲染器中嘗試了這個 protected override void
OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
{
base.OnElementChanged(e);
this.Control.TextLocale = Locale.CanadaFrench;
}
這些都沒有改變語言環境。
我還發現這篇文章似乎已經完成了將日歷視圖更改為不同的區域設置 Set Android DatePicker title language但不清楚它是如何完成的。
事實證明,您必須在設置中將法語添加為一種語言才能以法語顯示內容。
您只需要在 DatePicker android 自定義渲染器中使用這兩行即可使日歷成為法語
this.Control.TextLocale = locale;
Resources.Configuration.SetLocale(locale);
如果您的標題仍然是英文,請參考這篇文章Set Android DatePicker title language
Locale locale = new Locale("fr");
Control.TextLocale = locale;
Android.Content.Res.Configuration config = new Android.Content.Res.Configuration();
config.Locale = locale;
Locale.SetDefault(Locale.Category.Format, locale);
Resources.Configuration.SetLocale(locale);
Resources.Configuration.Locale = locale;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.