UWP 扩展前台会话

UWP Extended Foreground Session

因此,我尝试了一些使用 C++/CX 在 UWP 中创建应用程序的尝试,除了害怕语法之外,我还 一些 乐趣。 当我试图阻止应用程序被挂起时,我 运行 遇到了麻烦。最初我尝试使用 this 资源,但我了解到存在超时。然后我尝试摆脱所说的超时(虽然我不记得了,也找不到确切的资源)。

终于找到了holy grail,但是还是不行

    auto ses = ref new ExtendedExecutionForegroundSession();
    ses->Reason = ExtendedExecutionForegroundReason::Unconstrained;
    ses->Description = "Michael";
    auto res = create_task(ses->RequestExtensionAsync()).get();
    if(res == ExtendedExecutionForegroundResult::Allowed)
    {
        ses->Revoked += ref new TypedEventHandler<Object^,ExtendedExecutionForegroundRevokedEventArgs^>(this, &App::OnRevoked);
        MessageDialog^ messageDlg = ref new MessageDialog("Allowed to exec");
        auto sh = create_task(messageDlg->ShowAsync());
        sh.then([this](IUICommand^ comm) {});
        Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
    }
    
    

在此代码片段中,我创建了一个不受约束的会话扩展,如果获得许可,我还会显示一个消息框,因此我知道它有效。

无论是在调试还是在 运行 时,我都没有遇到任何问题,我 运行 使用此应用程序的笔记本电脑主要靠电池供电。但是挂起事件仍然触发,并没有调用任何撤销事件。

我将在此处提供两个代码片段:

void App::OnRevoked(Object^ sender, ExtendedExecutionForegroundRevokedEventArgs^ e)
{
    MessageDialog^ messageDlg = ref new MessageDialog(e->Reason.ToString());
    auto sh = create_task(messageDlg->ShowAsync());
    sh.then([this](IUICommand^ comm) {});
}
void App::OnSuspending(Object^ sender, SuspendingEventArgs^ e)
{
    MessageDialog^ messageDlg = ref new MessageDialog("Suspension event raised");
    auto sh = create_task(messageDlg->ShowAsync());
    sh.then([this](IUICommand^ comm) {});
}

与之前许多其他人一样,我的任务是找到防止这种行为的解决方法,因此我的问题恢复为:

如何防止 UWP 被挂起,而不考虑它 运行 正在使用的硬件(它会 运行 在 PC 上,无论是台式机还是笔记本电脑),并且没有时间或资源限制?

我解决了这个问题,我最终在 c# 中从头开始创建了一个新项目,并重新创建了整个项目(幸运的是我没有遇到太多麻烦,因为没有太多需要实施的东西)。最后,这可能与我的 xaml 或错误的 API 调用有关。

谢谢 Roy Li - MSFT 抽出时间。