A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Hello,
I need the close event of the application ?
Yes, if you want to detect the close button event in the title bar, you can get the appwindow for windows platform. And re-write the close event, after query operation finished, then close this application.
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureLifecycleEvents(events =>
{
//add here
#if WINDOWS
events.AddWindows(windowsLifecycleBuilder =>
{
windowsLifecycleBuilder.OnWindowCreated(window =>
{
var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
appWindow.Closing += async (s, e) =>
{
e.Cancel = true;
using HttpClient httpClient = new HttpClient();
// Compose and send the HTTP request
HttpResponseMessage responseCode = await httpClient.GetAsync("https://learn.microsoft.com/");
// Check the response status
if (responseCode.IsSuccessStatusCode)
{
// "query successful";
App.Current.Quit();
}
else
{
// query failed"
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.