【发布时间】:2016-12-01 17:08:33
【问题描述】:
大家好,我知道 app.xaml 文件中的 OnSuspending 函数允许我在我的应用程序暂停时保存信息。如果用户关闭应用程序,我希望我的用户在我的数据库中自动注销。这是我的代码:
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
if (CommonVariables.LoggedIn)
{
CommonVariables.LoggedIn = false;
string jsonPayload = "{\"user_id\":\"" + CommonVariables.AuthenticateUserResponseDetails.user.id + "\"}";
HttpClient client = new HttpClient();
string postUrl = CommonVariables.SERVER + CommonVariables.LogOut;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, postUrl);
//encrypt tase
EDecrypt encrypt = new EDecrypt();
jsonPayload = encrypt.AES_Encrypt(jsonPayload, CommonVariables.EncryptionKey);
request.Content = new StreamContent(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonPayload)));
var result = await client.SendAsync(request);
string response = string.Empty;
}
//TODO: Save application state and stop any background activity
deferral.Complete();
}
这对我很有效,所以当我的用户在他们的设备中关闭应用程序时,该函数被调用并且他们被注销。但是我的问题是,当用户最小化应用程序或将应用程序置于后台并进入另一个应用程序时,该功能也会将用户注销。那么如何调整我的代码,以便我的注销功能仅在用户关闭其设备中的应用程序时才起作用?
【问题讨论】:
-
你不能。 WinRT(又名 UWP)的核心功能是您的进程可以在暂停后立即终止。每当操作系统需要 RAM 用于其他进程时就会发生。 "instantly" 子句阻止任何与您要求的类似的事情。
标签: c# xaml windows-store-apps windows-8.1 windows-10-universal