[英]how to override windows back button in windows phone 8 ?
當我從我的應用程序點擊時,我需要將窗口后退按鈕。我嘗試了下面的方法,但它在Windows Phone 8中不起作用?
方法1)
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
var result = MessageBox.Show("Do you want to exit?", "Attention!",
MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
// base.OnBackKeyPress(e);
return;
}
e.Cancel = true;
}
方法2)
a)在xaml標題中寫道
BackKeyPress="MyBackKeyPress"
b)在構造函數中初始化它
BackKeyPress += OnBackKeyPress;
c)並調用此方法
private void MyBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true; //tell system you have handled it
//you c
}
這兩種方法都不起作用。 當我點擊后退按鈕時,應用程序被破壞,無法控制后門按鈕。 有幫助嗎?
根據認證要求,不建議覆蓋后退按鈕 -
從應用程序的第一個屏幕按“后退”按鈕必須關閉該應用程序。
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh184840%28v=vs.105%29.aspx
您可以嘗試此代碼
protected override void OnBackKeyPress(CancelEventArgs e)
{
if(MessageBox.Show("Are you sure you want to exit?","Confirm Exit?",
MessageBoxButton.OKCancel) != MessageBoxResult.OK)
{
e.Cancel = true;
}
}
試試這個,對我有用
第1步:在XAML中
BackKeyPress="PhoneApplicationPage_BackKeyPress"
第2步:在C#中
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult mRes = MessageBox.Show("Would you like to exit?", "Exit", MessageBoxButton.OKCancel);
if (mRes == MessageBoxResult.OK)
{
NavigationService.GoBack();
}
if (mRes == MessageBoxResult.Cancel)
{
e.Cancel = true;
}
}
在MyBackKeyPress
方法中,拋出一個新的異常以在運行時關閉應用程序並返回到手機的菜單。 我不知道這是否是實現它的好方法,但這肯定解決了這個問題。
private void MyBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
throw new Exception();
}
在Visual Studio
沒有Dubugging
情況下在模擬器/設備上進行Dubugging
,您將看到您的應用程序關閉。
我也試過了。 但它不起作用。 我把斷點放在代碼中,並且當我點擊后退按鈕時也發現它沒有進入或沒有調用該函數。 - 用戶1703145 45分鍾前
你有沒有忘記在XAML中綁定這個事件?
<phone:PhoneApplicationPage ..... BackKeyPress="phoneApplicationPage_BackKeyPress">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.