A lockscreen access throw request exception causes a hang or exception in mscorlib.dll

I use the following method to request lockscreen access in 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; } } 

This may give me two different errors. If I put a breakpoint before or in line

 status = await BackgroundExecutionManager.RequestAccessAsync(); 

the code will be executed, but throw the following exception:

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

As I read in another post, this is a bug known to others, I don't know about Microsoft. If I do not put a breakpoint in front of this line, execution will hang. What am I doing wrong here?

It seems that if I uninstall my application, it may work, but after some retries, it will eventually work again.

+4
source share
4 answers

There are two errors that I know when accessing screen lock access. Firstly, if you have a breakpoint on this line, execution will not be executed because your application does not work in the foreground (you are in Visual Studio and not in your application), and the lockscreen dialog cannot find the main ones windows of your application.

Another problem occurs when running in Simulator - every call to GetAccessStatus throws an exception because this call is not allowed in Simulator.

If you want to debug this, then put your breakpoint after calling GetAccessStatus and test it on your local machine and it should just work.

Update, I received this exception when the RequestAccessAsync method is called on a thread other than the UI. When you invoked the UI thread, it worked fine.

+6
source

Have you confirmed that your package manifest contains all the parameters necessary for an application with lock enabled?

I kept getting the exception thrown when calling GetAccessStatus. When checking the manifest file, I noticed that the "Screen Lock" setting was left blank. Installing it in the "Icon" or "Test of the sign and tiles" and selecting the logo of the icon, we solved the problem with the exception.

0
source

I have the same problem in snap mode (also in simulator and with breakpoint).

My solution is as follows:

  • Call RequestAccessAsync on the main page immediately after starting the application, for example, in the OnGotFocus method.
  • It should be called only once and should never be called in bind mode.
  • Do not try to execute it in an emulator.
  • Do not set a breakpoint for this method.

     private bool _isAccessRequested; protected override void OnGotFocus(RoutedEventArgs e) { if (!_isAccessRequested) { _isAccessRequested = true; BackgroundExecutionManager.RequestAccessAsync(); } } 
0
source

This should help you:

 async void MainPage_Loaded(object sender, RoutedEventArgs args) { var allowed = await Windows.ApplicationModel.Background .BackgroundExecutionManager.RequestAccessAsync(); System.Diagnostics.Debugger.Break(); } 

Good luck

0
source

Source: https://habr.com/ru/post/1436929/


All Articles