Hello,
What I want is, when the user clicks on the close or X button of the window I want to show a display alert asking do you want to close or not?
You can do this by adding a window close event to appwindow in the windowsLifecycleBuilder. When the user executes the closing method, we can push a display alert. If the user clicks Yes, close this application; if they click the Cancel, the display alert will dismiss. Here is a simple code that you can refer to.
#if WINDOWS
events.AddWindows(windowsLifecycleBuilder =>
{
windowsLifecycleBuilder.OnWindowCreated(window =>
{
//use Microsoft.UI.Windowing functions for window
var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
//When user execute the closing method, we can push a display alert. If user click Yes, close this application, if click the cancel, display alert will dismiss.
appWindow.Closing += async (s, e) =>
{
e.Cancel = true;
bool result = await App.Current.MainPage.DisplayAlert(
"Alert title",
"You sure want to close app?",
"Yes",
"Cancel");
if (result)
{
App.Current.Quit();
}
};
});
});
#endif
Best Regards,
Leon Lu
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.