本文介绍了UWP 在运行时更改 CalendarDatePicker 语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在运行时更改 UI 的应用程序.这是我更改语言的代码:

I have an application that changes UI in runtime. Here is my code for changing language:

public void SwitchLanguage(SupportedLanguage language)
{
    // Check if passed argument is different from current language
    if (CurrentLanguage != language.Type)
    {
        // Set the new current language
        CurrentLanguage = language.Type;

        // Override tha application primary language ( it will automatically save the language preference )
        Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = language.FourDigitCode;
        ResourceContext.GetForViewIndependentUse().Reset();
        ResourceContext.GetForCurrentView();

        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("LocalizedResourceMap"));
        // Notify code about the changes
        this.LanguageChanged?.Invoke(this, new EventArgs());
    }
}

所有本地化工作正常,除了 CalendarDatePicker - 它的 Flyout 没有本地化(在运行时,当我重新启动应用程序时 - 如果一切正常).

All localization works fine, except CalendarDatePicker - it's Flyout doesn't get localized ( in runtime, when i relaunch the app - all if fine ).

这里是例子

打开一个页面并选择 CalendarDatePicker:

Opened a page and selected CalendarDatePicker:

将语言切换为俄语:

我曾尝试这样做:

// Attach to LanguageChanged event - created in my own code
// And trigger this method inside CalendarDatePicker:
private void LanguageChanged(object sender, EventArgs e)
{
    this.Language = "ru-RU"; // Hardcoded value for test only
}

结果是这样的:

我也试图使()一切无效.还尝试触发 TemplateChild CalendarView Update 方法 - 没有用.有什么建议如何实现正常的语言更改吗?

感谢 Elvis Xia,注意到在代码中更改语言时,CalendarView 的大小会被搞砸,因为如果我这样做:

Thanks to Elvis Xia, have been noticed that on language change in code, the CalendarView size gets screwed, because if i do this:

this.calendar.Language = "ru-RU"
this.calendar.Height = 500;
this.calendar.Width = 500;

我会看到日期(搞砸了,但仍然):

I will get to see the dates ( screwed, but still ):

知道如何解决这个问题吗?

推荐答案

所以,作为一个肮脏和讨厌的修复,我在 XAML 中设置了 CalendarView 项目的高度和宽度.在 CalendarDatePicker 控件模板中.

So, as a dirty and nasty fix, i have set the CalendarView item height and width in XAML. Inside the CalendarDatePicker control template.

宽度应该小于高度大约 100 像素 - 一切正常;

Width should be smaller than height at about 100px - the all works fine;

<CalendarView Height="400" Width="300" x:Name="CalendarView" ... />

但这仍然不是解决方案

这篇关于UWP 在运行时更改 CalendarDatePicker 语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 10:54