如何处理 Windows Phone 7 上的后退按钮 [英] How to handle the back button on Windows Phone 7

查看:22
本文介绍了如何处理 Windows Phone 7 上的后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 windows phone 7 模拟器上,当按下硬件后退按钮时,默认行为是关闭您当前的应用程序.我想覆盖这个默认行为,以便它导航到我的应用程序中的上一页.

On the windows phone 7 emulator, when the hardware back button is pressed, the default behaviour is for it to close your current application. I want to override this default behaviour so that it navigates to the previous page in my application.

经过一些研究,似乎可以通过覆盖 OnBackKeyPress 方法来做到这一点,如下所示:

After some research, it seems it should be possible to do this by overriding the OnBackKeyPress method, like so:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    // do some stuff ...

    // cancel the navigation
    e.Cancel = true;
}

但是,单击后退按钮仍会关闭我的应用程序.在上述方法上放置断点表明它从未被调用.我的应用程序退出代码上有另一个断点,并且该断点 命中了.

However, clicking the back button still closes my application. Putting a breakpoint on the above method reveals that it is never called. I have another breakpoint on my application exit code, and this breakpoint is hit.

我还需要做些什么来拦截后退按钮吗?

Is there something else I need to do to intercept the back button?

推荐答案

看来不可能重写 OnBackKeyPress 方法来拦截后退键,除非您使用 Navigate 方法在之间移动应用程序中的页面.

It would appear that it's not possible to override the OnBackKeyPress method to intercept the back key unless you use the Navigate method to move between pages in your application.

我以前的导航方法是更改​​根视觉对象,例如:

My previous method of navigation was to change the root visual, like:

App.Current.RootVisual = new MyPage(); 

这意味着我可以将所有页面保存在内存中,因此我不需要缓存存储在它们上面的数据(一些数据是通过网络收集的).

This meant I could keep all my pages in memory so I didn't need to cache the data stored on them (some of the data is collected over the net).

现在看来我需要在页面框架上实际使用 Navigate 方法,它会创建我要导航到的页面的新实例.

Now it seems I need to actually use the Navigate method on the page frame, which creates a new instance of the page I'm navigating to.

(App.Current.RootVisual as PhoneApplicationFrame).Navigate(
                                    new Uri("/MyPage.xaml", UriKind.Relative)); 

一旦我开始使用这种方法进行导航,我就可以按照我的问题中描述的方式覆盖后退按钮处理...

Once I started navigating using this method, I could then override the back button handling in the way described in my question...

这篇关于如何处理 Windows Phone 7 上的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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