我使用以下方法在 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/