请求锁屏访问会在mscorlib.dll中引发异常挂起或抛出异常

And*_*eas 4 c# windows-8 windows-runtime

我使用以下方法在WinRT中请求锁屏访问:

public async void RequestLockScreenAccess()
    {
        var status = BackgroundExecutionManager.GetAccessStatus();
        if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.Denied)
            status = await BackgroundExecutionManager.RequestAccessAsync();
        switch (status)
        {
            case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
                _mainInfo.NotifyUser = "This app is on the lock screen and has access to Always-On Real Time Connectivity.";
                break;
            case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
                _mainInfo.NotifyUser = "This app is on the lock screen and has access to Active Real Time Connectivity.";
                break;
            case BackgroundAccessStatus.Denied:
                _mainInfo.NotifyUser = "This app is not on the lock screen.";
                break;
            case BackgroundAccessStatus.Unspecified:
                _mainInfo.NotifyUser = "The user has not yet taken any action. This is the default setting and the app is not on the lock screen.";
                break;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这可以给我2个不同的错误.如果我在之前或在线放置一个断点

status = await BackgroundExecutionManager.RequestAccessAsync();
Run Code Online (Sandbox Code Playgroud)

代码将执行,但抛出以下异常:

mscorlib.dll中发生未处理的"System.Exception"类型异常附加信息:找不到元素.(来自HRESULT的异常:0x8002802B(TYPE_E_ELEMENTNOTFOUND))

正如我在另一篇文章中读到的,这是其他人所知的错误,不了解微软.如果我没有在此行之前放置断点,则执行将挂起.我在这做错了什么?

似乎如果我卸载我的应用程序,它可能会工作,但在一些重新运行之后它最终会再次失败.

Mar*_*han 6

访问锁屏访问时,我知道有两个错误.首先,如果你在该行上有断点,那么执行将失败,因为你的应用程序没有在前台运行(你在Visual Studio中,而不在你的应用程序中),并且锁屏对话框找不到你应用程序的主窗口.

在Simulator中运行时会出现另一个问题 - GetAccessStatus上的每次调用都会引发异常,因为在Simulator中基本上不允许这种调用.

如果你想调试它,那么在GetAccessStatus调用之后放置你的断点并在本地机器上测试它,它应该工作.

更新,当在非UI线程上调用RequestAccessAsync方法时,我也遇到此异常.在UI线程上调用时,它工作正常.