重要的應用程式介面
瞭解如何在系統暫停應用程式時儲存重要的應用程式數據。 此範例會註冊 Suspending 事件的事件處理程式,並將字串儲存至檔案。
註冊暫止事件處理程式
註冊以處理 Suspending 事件,這表示您的應用程式應該在系統暫停之前儲存其應用程式數據。
using System;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
partial class MainPage
{
public MainPage()
{
InitializeComponent();
Application.Current.Suspending += new SuspendingEventHandler(App_Suspending);
}
}
Public NotInheritable Class MainPage
Public Sub New()
InitializeComponent()
AddHandler Application.Current.Suspending, AddressOf App_Suspending
End Sub
End Class
MainPage::MainPage()
{
InitializeComponent();
Windows::UI::Xaml::Application::Current().Suspending({ this, &MainPage::App_Suspending });
}
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::Foundation;
using namespace Windows::UI::Xaml;
using namespace AppName;
MainPage::MainPage()
{
InitializeComponent();
Application::Current->Suspending +=
ref new SuspendingEventHandler(this, &MainPage::App_Suspending);
}
暫停之前儲存應用程式數據
當您的應用程式處理 Suspending 事件時,它有機會將其重要的應用程式資料儲存在處理程式函式中。 應用程式應該使用 LocalSettings 記憶體 API 同步儲存簡單的應用程式資料。
partial class MainPage
{
async void App_Suspending(
Object sender,
Windows.ApplicationModel.SuspendingEventArgs e)
{
// TODO: This is the time to save app data in case the process is terminated.
}
}
Public NonInheritable Class MainPage
Private Sub App_Suspending(
sender As Object,
e As Windows.ApplicationModel.SuspendingEventArgs) Handles OnSuspendEvent.Suspending
' TODO: This is the time to save app data in case the process is terminated.
End Sub
End Class
void MainPage::App_Suspending(
Windows::Foundation::IInspectable const& /* sender */,
Windows::ApplicationModel::SuspendingEventArgs const& /* e */)
{
// TODO: This is the time to save app data in case the process is terminated.
}
void MainPage::App_Suspending(Object^ sender, SuspendingEventArgs^ e)
{
// TODO: This is the time to save app data in case the process is terminated.
}
釋放資源
您應該釋放獨佔資源和檔句柄,讓其他應用程式可以在您的應用程式暫停時存取它們。 獨佔資源的範例包括相機、I/O 裝置、外部裝置和網路資源。 明確釋放獨佔資源和檔句柄有助於確保其他應用程式可在您的應用程式暫停時存取它們。 當應用程式恢復運行時,它應該重新取得其獨佔資源和檔案控制代碼。
備註
每當使用者切換到另一個應用程式或桌面或 [開始] 畫面時,系統就會暫停您的應用程式。 每當使用者切換回您的應用程式時,系統就會恢復它。 當系統繼續您的應用程式時,變數和數據結構的內容與系統暫停應用程式之前的內容相同。 系統會將應用程式還原到它離開的位置,讓使用者看起來就像是在背景中執行一樣。
系統會在應用程式暫停時嘗試將應用程式及其數據保留在記憶體中。 不過,如果系統沒有將應用程式保留在記憶體中的資源,系統將會終止您的應用程式。 當使用者切換回已終止的暫停應用程式時,系統會傳送 Activated 事件,並應在其 OnLaunched 方法中還原應用程式資料。
系統不會在應用程式終止時通知應用程式,因此您的應用程式必須儲存其應用程式數據,並在暫停時釋放獨佔資源和檔句柄,並在終止後啟動應用程式時加以還原。
如果您在處理程式內進行異步呼叫,控件會立即從該異步呼叫傳回。 這表示,即使異步呼叫尚未完成,執行仍可從事件處理程式傳回,而且您的應用程式仍會移至下一個狀態。 在傳遞至事件處理程式的 EnteredBackgroundEventArgs 物件上,使用 GetDeferral 方法來延遲暫停,直到在返回的 Windows.Foundation.Deferral 物件上呼叫 Complete 方法為止。
延遲不會增加您在應用程式終止之前可執行程式碼的時間。 它只會延遲終止,直到呼叫延遲的 Complete 方法,或最後期限通過,以先。 若要延長暫停狀態中的時間,請使用 ExtendedExecutionSession
備註
為了改善 Windows 8.1 中的系統回應性,應用程式在暫停資源之後,會給予低優先順序的資源存取權。 為了支援這個新的優先順序,暫停操作的逾時將延長,因此應用程式在 Windows 上的正常優先順序擁有相當於 5 秒的逾時,或在 Windows Phone 上介於 1 到 10 秒之間。 您無法擴充或改變此逾時時間範圍。
使用 Visual Studio 進行偵錯的注意事項: Visual Studio 會防止 Windows 暫停附加至調試程式的應用程式。 這是允許使用者在執行應用程式時檢視 Visual Studio 偵錯 UI。 當您偵錯應用程式時,您可以使用 Visual Studio 將暫停事件傳送給應用程式。 請確定 偵錯位置 工具列正在顯示,然後按一下 暫停圖示。
相關主題