UWP MVVM: refresh page after change of language

2019-08-09 03:16发布

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?

标签: c# mvvm uwp
3条回答
看我几分像从前
2楼-- · 2019-08-09 04:00

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);
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-09 04:03

this line works very well for me.

 await Task.Delay(100);
 Frame.Navigate(this.GetType());
查看更多
虎瘦雄心在
4楼-- · 2019-08-09 04:03

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");
查看更多
登录 后发表回答