PrismLibrary/Prism · warning · DialogException

CanClose returned false

Error message

CanClose returned false

What it means

CloseDialogAsync first asks the dialog's IDialogAware controller whether it may close via CanCloseDialog(). If that returns false, Prism refuses to close and throws DialogException with the "CanClose returned false" message. This is the programmatic path of a user-cancelled close.

Solutions

  1. Catch DialogException around CloseDialogAsync and treat it as 'dialog declined to close'.
  2. Check the dialog controller's CanCloseDialog (or your VM's close-permission state) before requesting close.
  3. If the close should be forced, change the ViewModel so CanCloseDialog returns true for that path.
  4. Use RequestClose with an IDialogResult so the VM can signal why it refused.

Example fix

// before
await dialogService.CloseDialogAsync(dialog);
// after
try { await dialogService.CloseDialogAsync(dialog); }
catch (DialogException) { /* dialog's CanCloseDialog returned false — keep it open */ }
Defensive patterns

Strategy: try-catch

Validate before calling

// inspect your VM state before closing
bool mayClose = viewModel.CanCloseDialog();
if (!mayClose) return; // don't call CloseDialogAsync

Try / catch

try { await dialogService.CloseDialogAsync(); }
catch (DialogException ex) when (ex.Message.Contains("CanClose"))
{ /* dialog declined to close — keep it open */ }

Prevention

When it happens

Trigger: Calling dialogService.CloseDialogAsync (or RequestClose flow) on a dialog whose ViewModel's CanCloseDialog() returns false — typically because it has unsaved data or failed validation.

Common situations: Programmatically dismissing an edit form dialog whose CanCloseDialog returns false until the user saves; calling close from code without first checking whether the dialog permits closing; assuming CloseDialogAsync always succeeds.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/885245c096873d60. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Dialogs/DialogServiceBase.cs:153

            PageNavigationService.NavigationSource = PageNavigationSource.DialogService;

            result ??= new DialogResult();
            if (result.Parameters is null)
            {
                result = new DialogResult
                {
                    Exception = result.Exception,
                    Parameters = new DialogParameters(),
                    Result = result.Result
                };
            }

            var view = dialogModal.DialogView;
            var dialogAware = GetDialogController(view);

            if (!dialogAware.CanCloseDialog())
            {
                throw new DialogException(DialogException.CanCloseIsFalse);
            }

            PageNavigationService.NavigationSource = PageNavigationSource.DialogService;
            await dialogModal.DoPop(currentPage);
            PageNavigationService.NavigationSource = PageNavigationSource.Device;

            MvvmHelpers.InvokeViewAndViewModelAction<IActiveAware>(view, aa => aa.IsActive = false);
            MvvmHelpers.InvokeViewAndViewModelAction<IActiveAware>(currentPage, aa => aa.IsActive = true);
            dialogAware.OnDialogClosed();

            return result;
        }
        catch (DialogException)
        {
            throw;
        }
        catch (Exception ex)
        {

View on GitHub (pinned to 358118cd64)