We have a Xamarin UWP app that needs to be logged out whenever a user minimizes or clicks away from the window.
In my App.xaml.cs I have registered an event handler for the Suspending event. I then put our logout code in this event handler like so:
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.GetDeferral();
AppBackgrounded();
deferral.Complete();
}
This AppBackgrounded() method looks like this:
void AppBackgrounded()
{
if (!_isInBackgroundMode)
{
_isInBackgroundMode = true;
if (UserSetPin)
{
PinPage passcodePin = new PinPage();
Navigation.PushModalAsync(New NavigationPage(passcodePin), false);
}
else
{
App.Logout(null, true, true);
}
// clears the pasteboard so data can't be copied from this app into other apps
Clipboard.Clear();
}
}
We also have a AppLeavingBackground method that we use to restore the app when the user returns, but the app does not crash when returning. It only crashes when running the OnSuspended method.
This crash only occurs when
- The App is built for release and
- The device is in tablet mode
When in tablet mode, if you press the Task View button and navigate to another application the UWP app freezes trying to run through this code. If you try to return to the app, it will immediately exit.
I have tried to make the navigation to the other pages async and the app will then crash even when its not in tablet mode. I have also tried to put this logic in AppEnteredBackground and it still occurs.
This is hard to debug since it only occurs in release mode. Any ideas?