在Metro应用程序C#中重新加载页面

Raj*_*mar 21 c# xaml windows-8 windows-runtime

我正在使用Windows 8 RTM和C#(VS 2012 RTM)开发metro应用程序,我坚持使用页面重新加载,任何人都可以解释我如何重新加载页面而不再导航到同一页面. 简介:我正在开发具有多语言支持的metro应用程序.当用户选择语言时,我会通过以下代码覆盖主要语言

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "de";
Run Code Online (Sandbox Code Playgroud)

并使用此代码重新加载页面

this.Frame.Navigate(this.GetType());
Run Code Online (Sandbox Code Playgroud)

语言改为"de",但是当我在页面上按"返回"时导航相同的页面而不是导航到上一页.我想念一些东西,有人可以请你解释一下如何做到这一点.提前致谢

Jer*_*xon 16

这将刷新您的页面:

var _Frame = Window.Current.Content as Frame;
_Frame.Navigate(_Frame.Content.GetType());
_Frame.GoBack(); // remove from BackStack
Run Code Online (Sandbox Code Playgroud)
  • 处理OnNavigatingFrom()可以保存页面的数据和状态.
  • 处理OnNavigatingTo()可以加载页面的数据和状态.

作为警告,我的示例不考虑页面参数,您可能需要.此外,另一个警告,我的样本重新加载您的页面两次.但是从BackStack中删除新条目需要GoBack().与WP不同,Frame没有Refresh().此外,BackStack没有Remove().

UPDATE

我不再使用上述方法.我用这个:

public bool Reload() { return Reload(null); }
private bool Reload(object param)
{
    Type type = this.Frame.CurrentSourcePageType;
    if (this.Frame.BackStack.Any())
    {
        type = this.Frame.BackStack.Last().SourcePageType;
        param = this.Frame.BackStack.Last().Parameter;
    }
    try { return this.Frame.Navigate(type, param); }
    finally { this.Frame.BackStack.Remove(this.Frame.BackStack.Last()); }
}
Run Code Online (Sandbox Code Playgroud)

  • 贡献比批评要好得多 (8认同)
  • 本周最佳复出,男人.好的.我承认. (2认同)