UWP MVVM: refresh page after change of language

2019-08-09 03:19发布

问题:

I have some code in my view model which changes the application language, which then changes the text on some of the controls.

This is the DashboardViewModel, which the Dashboard Page's data context is set to:

ApplicationLanguages.PrimaryLanguageOverride = languageCode;
ResourceContext.GetForCurrentView().Reset();
ResourceContext.GetForViewIndependentUse().Reset();
NavigationService.Navigate(typeof(DashboardPage));

With NavigationService.Navigate(typeof(DashboardPage)); I tried to force the page to refresh, with no success. How would I do this?

回答1:

this line works very well for me.

 await Task.Delay(100);
 Frame.Navigate(this.GetType());


回答2:

NavigationService.Navigate() is not doing anything if you're trying to navigate to the same page.

On workaround is to add a parameter to your navigation request to force it.

NavigationService.Navigate(typeof(DashboardPage), "force refresh after language change");


回答3:

I have used an approach similar to @Vincent, but using DateTime.Now.Ticks as the parameter. This ensures that the value of the parameter is going to be different, which triggers the refresh.

NavigationService.Navigate(typeof(DashboardPage), DateTime.Now.Ticks);


标签: c# mvvm uwp