簡體   English   中英

如何更改 Xamarin.Forms 中日期選擇器的區域設置?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM