WPF/Silverlight NavigationService后退堆栈 [英] WPF/Silverlight NavigationService backstack

查看:100
本文介绍了WPF/Silverlight NavigationService后退堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF编写一个应用程序(也应该适用于Silverlight),其中我在主窗口中有一个Frame,并且正在使用NavigationService浏览该Frame的页面.

I'm writing an app using WPF (should apply to Silverlight too) where I have a Frame within a main Window and I'm using the NavigationService to navigate through the pages of the Frame.

我正在使用NavigationService.GoBack成功返回到上一页,但是有时我需要转到NavigationService堆栈中的特定页面.如果该页面在堆栈中不存在,那么我将简单地创建一个新页面并导航至该页面.通过这样做,我希望页面的状态在后堆栈中可以保留,但是如果没有,则会创建一个新的页面.

I am using NavigationService.GoBack to successfully return to the previous page but there are times when I will need to go to a specific page within the NavigationService backstack. If the page doesn't exist in the backstack then I will simply create a new page and navigate to it. By doing this, I'm hoping that the state of the page will be retained if it exists in the backstack, but if it doesn't a new one will be created.

这里的问题!

我似乎无法弄清楚如何访问后退堆栈以检查页面是否存在,然后如何导航到页面(如果存在).

I can't seem to work out how to access the backstack to check if the page exists, and then how to navigate to it if it does.

我还要提及的是,我不是使用URI进行导航,而是使用页面对象,因为我需要在页面之间传递值.

I'd also like to mention that I'm not using URI to navigate but page objects instead as I need to pass values between pages.

推荐答案

我似乎无法弄清楚如何访问后退堆栈以检查页面是否存在,然后如何导航到页面(如果存在).

I can't seem to work out how to access the backstack to check if the page exists, and then how to navigate to it if it does.

显然没有办法从NavigationService访问Backstack,但是Frame公开了BackStack属性,因此您可以执行以下操作:

Apparently there is no way to access the backstack from the NavigationService, but Frame exposes a BackStack property, so you can do something like:

if (frame.BackStack.Cast<object>().Contains(thePage))
...

但是,似乎没有一种方法可以直接跳转到历史记录中的特定条目...您可以做的最好的事情就是找到条目的索引,然后再返回N次以到达该位置.相当丑陋:

However there doesn't seem to be a way to jump directly to a specific entry in the history... The best you can do is find the index of the entry, and go back N times to get there, which is quite ugly:

int index = frame.BackStack.Cast<object>()
                 .Select((page, i) => new { page, i + 1 })
                 .Where(x => Equals(x.Page, thePage))
                 .FirstOrDefault();
if (index > 0)
{
    for(int i = 0; i < index; i++) frame.GoBack();
}
else
{
    frame.Navigate(thePage);
}

这篇关于WPF/Silverlight NavigationService后退堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆