我需要取消设备返回按钮事件。我已经尝试过在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中的相同问题。
发布于 2012-08-06 18:02:42
关于您的信息:如果您编写一个应用程序(不是XNA游戏),您应该避免取消回键。否则,您的应用程序将取消通过市场服务。
您还可以用相同的代码覆盖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‘解决方案。打开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,我已经在芒果设备和标准模拟器上检查过了。我觉得问题出在别的地方。也许当您试图取消back事件时,有些代码会使您的应用程序崩溃?
请试着在新的简单项目上检查一下。
https://stackoverflow.com/questions/11833421
复制相似问题