0

我需要取消设备后退按钮事件。我已经尝试过在Control press "back button" 中发布的解决方案,并使用确认对话框禁用关闭应用程序 - 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 侧 “后退按钮”事件的相同问题不会触发

4

1 回答 1

1

For your information: If you write an application (not an XNA game) you shoul avoid canceling back button. Otherwise your app will be canceled on passing Marketplace sertification.

Also you can override OnBackKeyPress method with the same code;

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;
        }

update

I've just created a new 'Silverlight for windows phone" solution. Opened MainPage.xaml.cs file and added this code to it:

// 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;
}

so there is only one page in this project. And it works. The target platform is Windows Phone OS 7.1, I've checked it on Mango device and on the standard emulator. I think the problem is somewhere else. Maybe some code crashes you application while you are trying to cancel back event?

Please, try to check it on new simple project.

于 2012-08-06T18:02:42.710 回答