microsoft / XboxGameBarSamples

Sample code for Xbox Game Bar Widgets
https://developer.microsoft.com/games/products/game-bar/
MIT License
145 stars 28 forks source link

[Question] How to intercept a closing window? #116

Closed YourFutureHusband closed 1 year ago

YourFutureHusband commented 2 years ago

I would like to implement a feature inside my application that detects when a user closes the game bar widget window (using the "X" button on the upper right), showing a simple prompt, similar to what other non UWP apps do:

0_80xGG3CZgVp6Fm8z

Windows Creators update (version 1703) added systemnavigationmanagerpreview exposing a closerequested event. Unfortunately this event does not trigger.

Is there a different solution?

vidager commented 2 years ago

The SDK does have this ability, but we didn't end up documenting it yet. However, it is supported.

Off the XboxGameBarWidget object there is a

event Windows.Foundation.TypedEventHandler<XboxGameBarWidget, XboxGameBarWidgetCloseRequestedEventArgs> CloseRequested;

runtimeclass XboxGameBarWidgetCloseRequestedEventArgs
{
    Windows.Foundation.Deferral GetDeferral();
    Boolean Handled{ get; set; };
}

If you subscribe to CloseRequested, it will call your handler before closing the widget. If you take a deferral (to perform async work), we'll wait for that deferral before checking that handled property. If you set Handled to true, we'll cancel our internal close and rely on your code to "do the right thing". If you return false, we'll proceed with the close.

vidager commented 1 year ago

Closing as there is a solution to the issue