Windows 11 - Can I toggle SystemNavigationManagerPreview CloseRequested ?
We have an app in the Microsoft store that has the confirmAppClose restricted capability.
As a result, when the user puts the app in the background and then tries to close it from the taskbar, nothing happens immediately (the app successfully closes, of course, when the user puts it in the foreground).
Ideally, we would like a close request to actually put the app in the foreground, so that the close can happen automatically after we clean up, but I suspect this is not currently possible (is that capability maybe in the works?)
Otherwise, we would like to be able to hook and unhook the CloseRequested event, so that at least we can get immediate responsiveness when there isn't anything to clean up!
But I have a test application in which I do
prev = SystemNavigationManagerPreview.GetForCurrentView(); prev.CloseRequested += PsaAccounting_CloseRequested;
and then later, in response to a button
prev.CloseRequested -= PsaAccounting_CloseRequested;
Yet even after pressing that button and seemingly unhooking the event (I checked with breakpoints in debug mode) the application does not close immediately, and the PsaAccounting_CloseRequested method is called (again, I checked with breakpoints in debug mode).
Why is this happening?
How can I make this stop?
I am sorry, that unsubscribe must have been some mistake on my part (my guess is that something in the process subscribed MORE than once). Unsubscribing works, at least as far as not firing the event goes. I had put the rescap/subscribe/unsubscribe into a small existing test app, with subscription or unsubscription happening every time a button was pressed, and the event kept firing after unsubscribe -- but when I repeated this process with a completely new App made purely for this purpose, the event stopped firing after unsubscribe.
The unsubscribe did NOT, however, really undo the effects of the subscribe.
Before the subscribe button is pushed, you can put the app into background, close it on the taskbar, and it closes immediately (of course, the App_CloseRequested is never called)
Before the subscribe button is pushed, you can put the app into background, close it on the taskbar, and nothing happens until you bring it to the foreground (then the App_CloseRequested is called and the app closes)
After the unsubscribe button is pushed, you can put the app into background, close it on the taskbar, and nothing happens until you bring it to the foreground (then the App_CloseRequested is no longer called and the app closes)
I would have hoped that unsubscribing would actually get you back to the initial state, and the App would close immediately as before.
I zipped up the test solution, but the attachment button won't take zip files. I can fake the extension if that is permissible.
Note that "ClosedRequested events will not be raised for windows that are non-visible" is no longer 100% accurate when the confirmAppClose rescap is in effect.
It becomes (my own wording) "**ClosedRequested events will not be raised while windows remain non-visible".
The event is actually raised (and queued, I guess?), it just doesn't actually get processed until the window becomes visible.
I'm more confused now. So your real question is that after unsubscribing the Close event, your app will not be closed immediately when the app is minimized, right?
@Fiederer, Jens JF Have you solved your issue? If not, would you like to share more information about the real question?
We are releasing our product despite not being able to close it immediately on the taskbar, that is not a show stopper.
I WOULD like to know why having once subscribed to the Close event, the app is permanently, despite unsubscribing, poisoned from closing immediately, but maybe I should make that a separate question with a more appropriate title.
I am actually trying to accept your first answer as an answer for the posted question (knowing that unsubscribe actually unsubscribed really helped at least remove the event, if not the effect on being able to close!) but am not seeing the advertised "Answer" button.
First time I was logged in as my personal account, and noticed I had posted under my business account...but I logged in and out and shut down the browser and refreshed the cache with ^F5, and still no Answer button!
@Fiederer, Jens JF So glad to hear that it helps you. I've converted as an answer, and you could accept it as answer now.
So that is why I couldn't earlier. My first question, thanks for the help!
Sign in to comment
Welcome to Microsoft Q&A!
Based on the document: SystemNavigationManagerPreview.CloseRequested Event, it has been mentioned that ClosedRequested events will not be raised for windows that are non-visible. So it is expected behavior that the event will not fire when close it from the taskbar.
And I've made a test about subscribe and unsubscribe the
CloseRequestedevent. On my side, when I click a button to unsubscribe the event, and then close the app by click the X button, the event will not fire again.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Sign in to comment
0 additional answers
Sort by: Most helpful