c# - 请求锁屏访问抛出异常挂起或在 mscorlib.dll 中抛出异常

标签 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;
        }
    }

这会给我 2 个不同的错误。如果我在行前或行上放置断点

status = await BackgroundExecutionManager.RequestAccessAsync();

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

An unhandled exception of type 'System.Exception' occurred in mscorlib.dll Additional information: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND))

正如我在另一篇文章中读到的,这是一个其他人都知道的错误,不知道微软。如果我不在此行之前放置断点,执行将挂起。我在这里做错了什么?

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

最佳答案

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

在模拟器中运行时会出现另一个问题——每次调用 GetAccessStatus 都会抛出异常,因为这个调用在模拟器中基本上是不允许的。

如果你想调试这个,然后在 GetAccessStatus 调用之后放置你的断点并在本地机器上测试它,它应该可以正常工作。

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

关于c# - 请求锁屏访问抛出异常挂起或在 mscorlib.dll 中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12650697/

相关文章:

c# - C# 构建的 .exe 是否可以 self 删除?

c# - 使用 [Flags] 属性标记枚举的原因是什么?

c# - 阅读中继器内的文本框

windows-8 - Windows 8 应用程序的广告提供商

mvvm - 从数据绑定(bind)属性 setter 中调用异步方法的正确方法?

c# - IHttpHandler.IsReusable 有什么用?

windows-8 - Windows 应用商店应用程序中不会调用 Page.OnNavieratedTo

c++ - typeinfo name() 和 endl 在 Windows 和 mingw 中不能一起工作

c# - 如何向用户请求使用 Windows 应用商店应用中的位置的权限?

fonts - 在 WinRT 中计算字体基线