Confirmation box cannot open in Mac Catalyst
Description
I have created Maui Blazor Server app and currently I are working on MacCatalyst application. I want to show confirmation dialog when user close the application using close button. So I bind the Window Destroying event and in that bind one function which can show the confirmation box. I used DisplayAlert() and manual designed xaml page show as popup, etc.. but that popups are not shown and application instance destroyed. In Windows app I have used MessageBox.Show() which will open the confirmation and page which is binded with screen is destroyed and screen becomes blank but still popup is visible. Although this would be fine same thing try to do in MACCatalyst but not working. Is there any possible way to show this confirmation popup when user close the application in MacCatalyst?
here is the code in MainPage.xaml for this scenario, Initialize event at blazorwebview initialization process,
private void BlazorWebView_Loaded(object sender, EventArgs e)
{
App.Current.MainPage.Window.Destroying += OnMainWindowClosed;
}
private async void OnMainWindowClosed(object sender, EventArgs e)
{
_commonEventhandlerService.InvokeDatabaseUpdateEvent(); //check file is updated
if (DatabaseStatus.IsModified)
{
#if WINDOWS
var result = MessageBox.Show(DataModels.Globalization.Resources.UnsavedMsg, DataModels.Globalization.Resources.Warning, MessageBoxButton.YesNo);
_commonEventhandlerService.InvokeSaveEvent(result == MessageBoxResult.Yes ? true : false); //save or close the files
#endif
#if MACCATALYST
//HERE I WANT TO OPEN CONFIRMATION POPUP
_commonEventhandlerService.InvokeSaveEvent(false);
#endif
_appOperationService.ShutDownApplication(); // process.kill(); close the app
}
else
{
_appOperationService.ShutDownApplication();
}
}
Steps to Reproduce
No response
Link to public reproduction project repository
No response
Version with bug
Unknown/Other
Is this a regression from previous behavior?
No, this is something new
Last version that worked well
Unknown/Other
Affected platforms
macOS
Affected platform versions
MacOS Sonoma 14.2.1
Did you find any workaround?
No response
Relevant log output
No response
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!
Closed similar issues:
- How to handle AppWindow closing event before close window on MacOS? (#20134), similarity score: 0.74
- MAUI DisplayAlert() When User Closes Window : Getting Unhandled Exception (#9355), similarity score: 0.73
- DisplayActionSheet hangs app on cancel on MacCatalyst (#6623), similarity score: 0.71
- [Mac Catalyst] Application.Current.OpenWindow not working (#10603), similarity score: 0.71
- DisplayAlert not working on MacCatalyst and UWP (#11766), similarity score: 0.70
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
Can you show a little more code that actually shows the dialog? How do you do that?
My suspicion is that it runs on a background thread or something and you'll need to maybe use this. Have you tried the Dispatcher and did that change the behavior at all?
@jfversluis I have tried this solutions with Dispatch and without also. Please review
1. Application.Current.MainPage.Dispatcher.Dispatch(async` () =>
await DisplayAlert("Confirmation", "Hello", "cancel")
);
2. var taskCompletionSource = new TaskCompletionSource<bool>();
var alertController = UIAlertController.Create("Confirmation", "Are you sure you want to close the app?", UIAlertControllerStyle.Alert);
alertController.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, action => _commonEventhandlerService.InvokeSaveEvent(true)));
alertController.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Cancel, action => _commonEventhandlerService.InvokeSaveEvent(false)));
UIApplication.SharedApplication.InvokeOnMainThread(async () =>
{
await UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewControllerAsync(alertController, true);
taskCompletionSource.SetResult(true);
});
// 6. Wait for the task completion source
var presentationSuccessful = await taskCompletionSource.Task;
// 7. Code to execute after successful/failed presentation (optional)
if (presentationSuccessful)
{
_appOperationService.ShutDownApplication();
}
else
{
// Code to handle failed popup presentation (optional)
}
```
I think you need to create a small repro project for what you're trying to do. From the looks of it, you're calling into straight UIKit code for showing your dialog, which is fine to do but that's not really a MAUI UI issue or bug.
@drasticactions We have tight deadline and due to that not able to create small project. Can you just tell me is there any way to open confirmation popup when user close the application in Mac Catalyst?? I have already done for windows app using MessageBox.
@dhruvindudhat Something like this? https://github.com/steventroughtonsmith/CatalystUnsavedChanges?tab=readme-ov-file
If you want something to appear when a user tries and closes a window (such as a dialog box asking them if they want to close it), the short answer is you can't do that in Mac Catalyst. It's not supported at a platform level. Since you can't do it at a base platform level (With Objective-C or Swift), nothing you can do within .NET Mac Catalyst or MAUI will do it.
The longer answer is "If you access undocumented APIs you can force it to work," but those are, in essence, hacks that rely on undocumented APIs. It's doable, but not something that could be supported in the MAUI UI framework itself as it relys on hacks to work. Utill Apple creates public APIs that can allow that to work, there's nothing in MAUI that could be implmented.
Hi @dhruvindudhat. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md
This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
This issue has been automatically marked as stale because it has been marked as requiring author feedback to reproduce the issue but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.