如何获取当前页面的类型并在Windows Phone 8中刷新它

v.g*_*.g. 3 c# windows-phone windows-phone-8 windows-8.1 windows-phone-8.1

在我的Windows Phone 8.1应用程序中,我想获取当前页面类型,如果它与我的检查匹配以刷新该页面.怎么做?这不起作用:

Frame rootFrame = Window.Current.Content as Frame;
Run Code Online (Sandbox Code Playgroud)

yas*_*sen 5

你可以这样得到它:

var pageType = (Window.Current.Content as Frame).Content.GetType();
Run Code Online (Sandbox Code Playgroud)

关于提神:

如果您只想刷新页面(如果它是特定类型)(让我们称之为MyPage),您可以执行以下操作:

var page = (Window.Current.Content as Frame).Content as MyPage;
if (page != null) {
    page.Refresh(); //This is a method that you implement in the page, that refreshes it
}
Run Code Online (Sandbox Code Playgroud)

如果您要刷新几种类型的页面,请使用一个名为Refresh and use的方法创建一个接口,as IMyInterface而不是as MyPage在上面的示例中.

现在,如果你真的想通过导航刷新(这似乎不是最好的主意),你可以这样做:

var frame = (Frame)Window.Current.Content;
frame.Navigate(typeof(MyPage));
frame.BackStack.RemoveAt(frame.BackStack.Count - 1); //Or RemoveAt(0), haven't tested it
Run Code Online (Sandbox Code Playgroud)

但是这种方法在某些情况下不起作用.