问题
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());
}
}
All localization works fine, except CalendarDatePicker - it's Flyout doesn't get localized ( in runtime, when i relaunch the app - all if fine ).
Here are the examples
Opened a page and selected CalendarDatePicker:
Switched the language to Russian:
I have tried to do this:
// 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
}
And the result is this:
I have also tried to invalidate() everything. Also tried to trigger TemplateChild CalendarView Update method - no use. Any suggestions how to achieve normal language change?
EDIT:
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 ):
Any ideas how to fix this?
回答1:
So, as a dirty and nasty fix, i have set the CalendarView item height and width in XAML. Inside the CalendarDatePicker control template.
Width should be smaller than height at about 100px - the all works fine;
<CalendarView Height="400" Width="300" x:Name="CalendarView" ... />
But still this is not a solution
来源:https://stackoverflow.com/questions/40677814/uwp-change-calendardatepicker-language-in-runtime