简体   繁体   English

Windows Phone应用程序中的“后退”按钮控件

[英]Back button control in windows phone app

I am developing a windows phone 8 app. 我正在开发Windows Phone 8应用程序。 I want to control the back button of the phone for doing specific task. 我想控制手机的后退按钮以执行特定任务。 I want that when user press the back button in specific page it will not navigate to the previous page but to the page which I want. 我希望当用户在特定页面中按后退按钮时,它不会导航到上一页,而是导航到我想要的页面。 Is their any way to control the hardware back button present in phone? 他们有什么方法可以控制手机中的硬件后退按钮吗?

In Silverlight apps (WP7, WP8, WP8.1) you do this: 在Silverlight应用程序(WP7,WP8,WP8.1)中,您可以执行以下操作:

protected override void OnBackKeyPress(CancelEventArgs e)
{
    // put any code you like here
    MessageBox.Show("You pressed the Back button");
    e.Cancel = true;                       
}

That will work in all Windows Phone versions if you're using Silverlight. 如果您使用的是Silverlight,则在所有Windows Phone版本中都可以使用。


If you're using WinRT for Windows Phone 8.1, it is a bit different: 如果您将WinRT用于Windows Phone 8.1,则有些不同:

Open NavigationHelper.cs and make this modification: 打开NavigationHelper.cs并进行以下修改:

private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    if (this.GoBackCommand.CanExecute(null) && !e.Handled)
    {
        e.Handled = true;
        this.GoBackCommand.Execute(null);
    }
}

Now in your app page (the page that will be open when the back button is pressed), add the following namespace: 现在在您的应用程序页面(按下后退按钮将打开的页面)中,添加以下名称空间:

using Windows.Phone.UI.Input;

Add this handler to the constructor method of your page: 将此处理程序添加到页面的构造方法中:

HardwareButtons.BackPressed += OnBackPressed;

Then add this method: 然后添加此方法:

private async void OnBackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    e.Handled = true;
    // add your own code here to run when Back is pressed
}

Note: in both cases, the 'e.Handled = true' line tells the OS that the back button press has been handled, and therefore the OS will not action the default behaviour. 注意:在两种情况下,“ e.Handled = true”行都告诉操作系统已处理了后退按钮的按下操作,因此操作系统将不执行默认行为。 If you remove that line your own code will run, and the OS will also do its own backwards navigation. 如果删除该行,则将运行您自己的代码,并且操作系统也将执行其自己的向后导航。

Be mindful of Rowland's comment about overriding the Back button - if you're not navigating intuitively you will confuse the user and risk your game being rejected (if you just need to control a pause screen or menu it will be fine, but if you implement something gimmicky like using the Back button as a game control you'll be in trouble). 请记住Rowland关于覆盖“后退”按钮的评论-如果您不直观地进行导航,则会使用户感到困惑,并有可能导致游戏被拒绝(如果您只需要控制暂停屏幕或菜单,那很好,但是如果您实施有点花哨的东西,例如使用“后退”按钮作为游戏控件,您会遇到麻烦)。

My blog has the same answer with a bit more detail if you need it: http://grogansoft.com/blog/?p=572 如果您需要,我的博客也提供了相同的答案,但有更多详细信息: http : //grogansoft.com/blog/?p=572

Whilst it possible to cancel the navigation event, and permissable in a game to present a pause screen or similar, generally it is not allowed to use the back button for anything other than backward navigation in an app; 尽管可以取消导航事件,并且在游戏中允许显示暂停屏幕或类似内容,但通常不允许将后退按钮用于应用程序中的向后导航以外的任何操作; Per requirement 5.2.4 of the Technical certification requirements for Windows Phone 根据Windows Phone技术认证要求的要求5.2.4

To maintain a consistent user experience, the Back button must only be used for backwards navigation in the app. 为了保持一致的用户体验,“后退”按钮只能用于应用程序中的向后导航。

If you are creating a XAML app where it is permissible to cancel a "back" operation, such as per 5.2.4.4 of the Technical certification requirements for Windows Phone : 如果要在XAML应用中创建允许取消“后退”操作的应用,例如,根据Windows Phone的技术认证要求5.2.4.4:

For games, when the Back button is pressed during gameplay, the game can choose to present a pause context menu or dialog, or it can navigate the user to the prior menu screen. 对于游戏,在玩游戏时按下“后退”按钮,游戏可以选择显示一个暂停上下文菜单或对话框,或者可以将用户导航到上一个菜单屏幕。

Then you can implement this by overriding the OnNavigatingFrom method on your page, and set the Cancel property of the NavigatingCancelEventArgs , so something like this example from Frame, page, and navigation features for Windows Phone 8 : 然后,您可以通过重写页面上的OnNavigatingFrom方法来实现此OnNavigatingFrom ,并设置NavigatingCancelEventArgsCancel属性,以便像Windows Phone 8的Frame,页面和导航功能中的此示例所示:

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    base.OnNavigatingFrom(e);

    // If the navigation can be cancelled, ask the user if they want to cancel
    if (e.IsCancelable)
    {
        MessageBoxResult result = MessageBox.Show("Do you want to stay here?", "Confirm Navigation from Page", MessageBoxButton.OKCancel);
        if (result == MessageBoxResult.OK)
        {
            // User wants to stay here
            e.Cancel = true;
            return;
        }
    }
}

Of course, you may choose to implement the prompt differently, but that should illustrate how it is possible. 当然,您可以选择以其他方式实施该提示,但这应该说明它是如何实现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM