2012-08-06 127 views
0

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

这里是我的代码...

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

也看到,同样的问题从科尔多瓦侧 "backbutton" event won't fire

回答

1

为了您的信息:如果你编写一个应用程序(不XNA游戏),你建议立即进行删除避免取消后退按钮。否则,您的应用将在通过Marketplace认证时取消。

你也可以用相同的代码覆盖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; 
     } 

更新

我刚刚创建了Windows Phone的”解决方案一个新的” Silverlight的打开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设备和标准模拟器上进行了检查,我认为这个问题在其他地方,也许某些代码会使你的应用程序崩溃当你试图取消回来的事件?

请尝试检查新的简单项目。

+0

感谢您的信息。我确实想使用硬件后退按钮。我只是想一次一件事地做事。最后,我需要按照后退事件,然后使用JavaScript执行导航。在我这样做之前,我需要取消默认的按钮行为。 – 2012-08-06 18:14:07

+0

好吧,无论如何,你的代码适合我。你有没有试图在Debian中使用OnBackKeyPressed方法?当您按取消按钮时,代码是否转到e.Cancel = true语句? – 2012-08-06 18:18:03

+0

我的代码确实去e.Cancel并将其设置为true,但应用程序仍然退出。 – 2012-08-06 18:21:34