[英]Application.Current.Exit in Windows Phone 8.1 doesn't terminate app
我應該如何以編程方式“適當地”終止Windows Phone 8.1應用程序? Application.Current.Exit ();
不起作用。 我的代碼將一直繼續下去。 這是代碼段:
public static async Task ShowAndGo (String MessCode, String MessText, Boolean Xit)
{
String Mess = ""; // Start out with an empty Message to tell Joe User.
String Title = ""; // And an empty title too.
MessageDialog messageDialog = new MessageDialog (Mess, Title);
await messageDialog.ShowAsync (); // Stop and wait for user to acknowledge.
if (!Xit) // We're done here if we're not goin' down.
return;
//
// If we're goin' down,
// we have a lot of cleanup to do.
//
Application.Current.Exit (); // This should stop us. But it doesn't.
}
簡而言之:沒有“正確”的方法,建議您不要
關於Application.Exit方法的注釋指出:
但是,通常不應提供此UI,因為系統會自動管理應用程序的生命周期並根據需要終止掛起的應用程序以釋放資源。
該准則要求您:
將您的應用設計為在用戶離開它時掛起,並在用戶切換回它時恢復。
當用戶離開應用程序或使用關閉按鈕關閉應用程序時,請勿終止應用程序。 操作系統確保用戶以一致的方式訪問和管理應用程序。 當您的應用不再對用戶可見時,您的應用將被暫停。 通過將應用程序生命周期留給系統,可以確保用戶可以盡可能高效地返回到您的應用程序。 這樣做還可以提供設備的最佳系統性能和電池壽命。
而是為Suspending事件注冊一個事件處理程序。
當應用暫停時會調用它。 您可以使用此事件處理程序將相關的應用程序和用戶數據保存到持久性存儲中。
最好閱讀“ 應用程序生命周期”頁面。
Windows Phone 8.1
Application.Current.Exit();
Windows Phone 8.1(Silverlight)
Application.Current.Terminate();
我按原樣使用您的代碼進行測試,刪除后它按預期工作:
if (!Xit) // We're done here if we're not goin' down.
return;
Xit
永遠不會在您的代碼示例中分配; 如果Xit
為false
,則將無法到達終端線。 請記住,默認情況下, bool
值是false
的(如果沒有給它們賦值)
嘗試使用Application.Current.Terminate();
而不是Application.Current.Exit ();
例如
private void Exit_Button(object sender, System.Windows.Input.GestureEventArgs e)
{
Application.Current.Terminate();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.