按下后退按钮时跳过一页,WP7 [英] Skip a page when the back button is pressed, WP7

查看:21
本文介绍了按下后退按钮时跳过一页,WP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在当前发布的 WP7 版本中,您无法对页面堆栈进行编程控制.

In the current released version of WP7 you have no programmatic control over the page stack.

我从 A 开始,转到 B,然后是 C.B 是新项目的数据输入页面,所以从 CI 回来希望逻辑上落在 A.目前我在 B 上监听来自 C 和强制另一个回到 A.但是,该事件不会很快发生,无法阻止页面在屏幕上显示.

I start at A, go to B, and then C. B is the data entry page for new items, so coming back from C I want to logically land at A. Currently I listen for navigation on B that comes from C and force another back onto A. However, the event doesn't happen soon enough to stop the page from displaying on-screen.

A -> B -> C

A -> B -> C

C -> A

A 是顶级列表页面.B 是新项目页面.C 是商品详情页.

A is the top level list page. B is the new item page. C is the item detail page.

到目前为止,这只发生在一种表单上,所以我的解决方法是覆盖我想跳过的页面中的 OnNavigatedTo,并以编程方式调用返回".但是,这会产生不希望的效果,即短暂显示页面然后立即导航离开.

This only occurs on one form so far so my workaround is to override OnNavigatedTo in the page I want to skip, and call "go back" programmatically. However, this has the undesired effect of briefly showing the page and then immediately navigating off of it.

  • 有没有可行的方法来阻止闪烁?
  • 我的解决方法应该是完全控制页面导航,包括返回吗?这将使页面堆栈处于一种奇怪的状态,但如果我控制所有,它将对用户隐藏导航.
  • Is there a workable way to stop the flicker?
  • Should my workaround instead be to take full control of page navigation, including go backs? This will leave the page stack is a strange state, but that would be hidden from the user if I control all navigation.

我知道 Mango 中有一个新功能可以以编程方式从页面堆栈中弹出页面,但我很想知道发布版本中是否有解决该问题的方法.

I know there is a new feature in Mango to pop a page from the page stack programmatically, but I'm curious to know if there is a solution to the problem in the released version.

这样做的另一个动机是我不知道哪个版本最适合定位,是最新的还是刚好适合应用程序的版本.目前我坚持使用现场版.

Another motivation for this is I don't know what version will be best to target, the latest, or the one that is just enough for the app. For the time being I'm sticking with the live version.

推荐答案

我通过在跳过期间使根框架透明来停止闪烁.这个例子不是直接来自我的代码.

I have stopped the flickering by making the root frame transparent for the duration of the skip. This example isn't straight from my code.

首先在你想跳过的页面上,覆盖 OnNavigatedTo 并测试你来自哪里(这是我的代码具体的地方,我会跟踪我的位置):

Firstly on the page you wish to skip, override OnNavigatedTo and test to see where you have come from (this is where my code gets specific, I keep track of where I am):

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    // If I've come from page C, go back again.
    NavigationService.GoBack();
}

其次,在主 App.xaml.cs 中为 Navigating 注册一个事件处理程序(我把它放在 public App() 构造函数中):

Secondly, in the main App.xaml.cs register an event handler for Navigating (I put it in public App() constructor):

RootFrame.Navigating += RootFrame_Navigating;

最后,将它们充实以隐藏框架并在跳过期间再次显示:

Finally, flesh them out to hide the frame and show it again for the duration of the skip:

    private bool _skipped;

    private void RootFrame_Navigated(object sender, NavigationEventArgs e)
    {
        RootFrame.Opacity = 100;
        RootFrame.Navigated -= RootFrame_Navigated;
    }

    private void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        if (_skipped)
        {
            _skipped = false;
            RootFrame.Navigated += RootFrame_Navigated;
        } 

        if (e.NavigationMode == NavigationMode.Back &&
            e.Uri.OriginalString.Contains("ThePage.xaml"))
        {
            RootFrame.Opacity = 0;
            _skipped = true;
        }
    }

确定何时使页面透明的代码在我的实际代码中也有所不同,但我在答案中添加了一个实现以供说明.这与另一个答案中链接的配方中的代码几乎相同,但不涉及需要使用第三方 DLL.

The code to determine when to make the page transparent is also different in my actual code, but I've added an implementation to the answer for illustration. This is almost identical to the code in the recipe linked in another answer, but doesn't involve needing to use a third party DLL.

我提供了我自己的答案,因为我之前看过其他答案中提供的来源,但从未关注隐藏根框架的代码.我没有使用非线性导航服务,只是用于框架透明度的代码片段(我不需要它来检测圆形导航,因为我很清楚我在应用程序中所做的设计选择并且可以自己发现它们:-)

I provided my own answer because I've seen the sources provided in the other answers before, but never paid attention to the code for hiding the root frame. I am not using the Non-Linear Navigation Service, just the code fragment for frame transparency (I don't need it to detect circular navigation as I'm well aware of the design choices I make in the app and can spot them myself :-)

在(当前)一种情况下,这足以作为一种解决方法,我需要跳过返回时没有意义的页面.我想当 Mango 出来时,我将最适合定位到最新版本,所以这段代码很快就会失效.

This suffices as a workaround in the (currently) one case I have where I need to skip a page that doesn't make sense when going back. I'd like to think when Mango comes out I will be best placed targeting the latest version, so this code will soon be defunct.

来源:

这篇关于按下后退按钮时跳过一页,WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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