gpt4 book ai didi

c# - Windows 10 移动应用程序扩展执行不断被拒绝

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:05 24 4
gpt4 key购买 nike

我正尝试在我的 Windows 10 UWP 应用程序上使用扩展执行来进行位置跟踪。网上有很多这样的例子。我基本上是在使用这个通用代码块。

using (var session = new ExtendedExecutionSession())
{
session.Reason = ExtendedExecutionReason.LocationTracking;
session.Description = "Tracking your location";
session.Revoked += NewSession_Revoked;
var result = await session.RequestExtensionAsync();

switch (result)
{
case ExtendedExecutionResult.Allowed:
await StartLocationTrackingAsync();
break;
default:
case ExtendedExecutionResult.Denied:
//Notify user or log this.
break;
}
}

此代码块在我的应用程序的 Suspending 事件处理程序中执行,如下所示:

public Scenario1()
{
this.InitializeComponent();
Application.Current.Suspending += Current_Suspending;
}

private async void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
//Do my extended execution request here.
}

我遇到的第一个问题是我什至无法触发该事件,但我读到 VS 不会在调试时暂停您的应用程序。所以,我强制它暂停使用 VS 中的生命周期事件下拉列表。现在,我每次都引发暂停事件,这很好。

但是,当我请求扩展执行 session 时,每次都被拒绝。我已经在模拟器和我的物理 W10 移动设备上尝试过多次,每次都被拒绝。

这是为什么?我怎样才能让它被允许?

谢谢!

最佳答案

假设您已经在应用程序 list 中声明了 Location,您将必须在前台运行代码,这意味着它将在您的 MainPage_Loaded 中运行 回调但处于挂起状态。

A location tracking extended execution session can run as long as needed. However, there can only be one such session running per device. A location tracking extended execution session can only be requested in the foreground, and the app must be in the Running state. This ensures that the user is aware that the app has initiated an extended location tracking session.

要在后台运行它,请看这个 -

It is still possible to use the GeoLocator while the app is in the background by using a background task, or an app service, without requesting a location tracking extended execution session.

当您使用 LocationTracking 扩展执行时 -

Specify ExtendedExecutionReason.LocationTracking when you create an ExtendedExecutionSession if your app needs to regularly log the location from the GeoLocator. Apps for fitness tracking and navigation that need to regularly monitor the user's location and should use this reason.

这是 full article对此。

关于c# - Windows 10 移动应用程序扩展执行不断被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44421264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com