4

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 can give me 2 different errors. If i place a breakpoint before or on line

status = await BackgroundExecutionManager.RequestAccessAsync();

the code will execute, but throw the following exception:

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

As i read in another post, this is a bug known to others, don't know about Microsoft. If i don't place a breakpoint before this line, execution will instead hang. What am i doing wrong here?

It seems that if i uninstall my application, it might work, but then after some reruns it will eventually fail again.

4 Answers 4

6

There are two bugs I know when accessing for a lockscreen access. First one, if you have breakpoint on that line, then the execution will fail because your application is not running in foreground (you are in Visual Studio, not in your app) and the lockscreen dialog cannot find main windows of your app.

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

If you want to debug this, then place your breakpoint after the GetAccessStatus call and test it on Local machine and it should just work.

Update, I was also getting this exception when the RequestAccessAsync method is called on non-UI thread. When called on UI thread, it worked fine.

2
  • Okay thanks, that makes sense. But im not running in simulator. After i moved the execution into a button eventhandler, it worked. But of course i want to request for lockscreen access automatically. is there something else i should know?
    – Andreas
    Commented Sep 29, 2012 at 8:16
  • Type library errors can be induced when a call needs to marshaled to another thread. Be very sure that you only run this code on the program's main UI thread. Commented Sep 29, 2012 at 12:02
0

Have you verified that your package manifest has all the settings needed for a lock-screen enabled app?

I kept getting an exception thrown when calling GetAccessStatus. Upon inspecting the manifest file I noticed the "Lock screen notifications" setting was left blank. Setting it to "Badge" or "Badge and tile test" and selecting a badge logo solved the exception issue.

0

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

My workaround is follow:

  1. Call RequestAccessAsync on main page right after app startup for example in OnGotFocus method.
  2. It should be called only once and never called in snapped mode.
  3. Don't try to execute it in emulator.
  4. Don't set breakpoint on this method.

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

This should help you:

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

Best of luck!

1
  • Because you cannot be sure that after that arbitrary 1000 ms delay, the following method call won't throw an exception. It's not a recommendable solution, even if it could work.
    – mcont
    Commented Aug 5, 2014 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.