Windows Phone 7取消硬件后退按钮不起作用

时间:2012-08-06 17:57:06

标签: c# windows-phone-7 cordova-2.0.0

我需要取消设备后退按钮事件。我已尝试在Control press "back button" and disable close the application using a dialog for confirm - wp7中发布的解决方案,但它对我不起作用。难道我做错了什么?无论是否从对话框中选择取消,应用程序始终退出。

这是我的代码......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Resources;


namespace GodTools
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.CordovaView.Loaded += CordovaView_Loaded;

            BackKeyPress += OnBackKeyPressed;

        }

        private void CordovaView_Loaded(object sender, RoutedEventArgs e)
        {
            this.CordovaView.Loaded -= CordovaView_Loaded;
            // first time load will have an animation
            Storyboard _storyBoard = new Storyboard();
            DoubleAnimation animation = new DoubleAnimation()
            {
                From = 0,
                Duration = TimeSpan.FromSeconds(0.6),
                To = 90
            };
            Storyboard.SetTarget(animation, SplashProjector);
            Storyboard.SetTargetProperty(animation, new PropertyPath("RotationY"));
            _storyBoard.Children.Add(animation);
            _storyBoard.Begin();
            _storyBoard.Completed += Splash_Completed;
        }

        void Splash_Completed(object sender, EventArgs e)
        {
            (sender as Storyboard).Completed -= Splash_Completed;
            LayoutRoot.Children.Remove(SplashImage);
        }

        void OnBackKeyPressed(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var result = MessageBox.Show("Do you want to exit?", "Attention!",
                                          MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                // Do not cancel navigation
                return;
            }
            e.Cancel = true;
        }
    }
}

另见Cordova方面的问题 "backbutton" event won't fire

1 个答案:

答案 0 :(得分:1)

供您参考:如果您编写应用程序(不是XNA游戏),则应避免取消后退按钮。否则,您的应用将在通过Marketplace sertification时被取消。

您也可以使用相同的代码覆盖OnBackKeyPress方法;

protected override void OnBackKeyPress(CancelEventArgs e)
        {
            var result = MessageBox.Show("Do you want to exit?", "Attention!",
                                          MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                base.OnBackKeyPress(e);
                return;
            }
            e.Cancel = true;
        }

<强>更新

我刚刚创建了一个新的'Silverlight for windows phone'解决方案。打开MainPage.xaml.cs文件并将此代码添加到其中:

// Constructor
public MainPage()
{
    InitializeComponent();
    BackKeyPress += OnBackKeyPressed;
}

void OnBackKeyPressed(object sender, System.ComponentModel.CancelEventArgs e)
{
    var result = MessageBox.Show("Do you want to exit?", "Attention!",
                                  MessageBoxButton.OKCancel);

    if (result == MessageBoxResult.OK)
    {
        // Do not cancel navigation
        return;
    }
    e.Cancel = true;
}

所以这个项目只有一个页面。它有效。目标平台是Windows Phone OS 7.1,我已经在Mango设备和标准模拟器上进行了检查。我认为问题出在其他地方。当你试图取消回事件时,某些代码可能会崩溃你的应用程序?

请尝试在新的简单项目上进行检查。