lepoco/wpfui · error · InvalidOperationException

Use {nameof(ShowDialogAsync)} instead

Error message

Use {nameof(ShowDialogAsync)} instead

What it means

MessageBox.Show() is hidden ('new') and hard-throws to forbid the legacy Window.Show entry point. WPF UI's MessageBox is task-driven (TaskCompletionSource) and must be opened via ShowDialogAsync, which wires up the result TCS, the title-bar/mica setup, and cancellation. Calling the base Show bypasses all of that, so the library blocks it.

Source

Thrown at src/Wpf.Ui/Controls/MessageBox/MessageBox.cs:285

    {
        Topmost = true;
        SetValue(TemplateButtonCommandProperty, new RelayCommand<MessageBoxButton>(OnButtonClick));

        PreviewMouseDoubleClick += static (_, args) => args.Handled = true;

        Loaded += static (sender, _) =>
        {
            var self = (MessageBox)sender;
            self.OnLoaded();
        };
    }

    protected TaskCompletionSource<MessageBoxResult>? Tcs { get; set; }

    [Obsolete($"Use {nameof(ShowDialogAsync)} instead")]
    public new void Show()
    {
        throw new InvalidOperationException($"Use {nameof(ShowDialogAsync)} instead");
    }

    [Obsolete($"Use {nameof(ShowDialogAsync)} instead")]
    public new bool? ShowDialog()
    {
        throw new InvalidOperationException($"Use {nameof(ShowDialogAsync)} instead");
    }

    [Obsolete($"Use {nameof(Close)} with MessageBoxResult instead")]
    public new void Close()
    {
        throw new InvalidOperationException($"Use {nameof(Close)} with MessageBoxResult instead");
    }

    /// <summary>
    /// Displays a message box
    /// </summary>
    /// <returns><see cref="MessageBoxResult"/></returns>

View on GitHub (pinned to ffebacd610)

Solutions

  1. Replace messageBox.Show() with await messageBox.ShowDialogAsync(showAsDialog: false).
  2. For a non-modal display, pass showAsDialog: false to ShowDialogAsync.
  3. Search the codebase for '.Show()' on MessageBox instances and update each.
  4. Treat the [Obsolete] attribute as a compile-time prompt — address it before runtime.

Example fix

// before
var box = new MessageBox { Content = "hi" };
box.Show(); // throws

// after
var box = new MessageBox { Content = "hi" };
await box.ShowDialogAsync(showAsDialog: false);
Defensive patterns

Strategy: validation

Validate before calling

// Replace box.Show() with the task-based API:
await box.ShowDialogAsync(showAsDialog: false);

Prevention

When it happens

Trigger: Calling messageBox.Show() — the no-arg instance method inherited from Window — on a WPF.UI MessageBox instance; reflection/automation invoking Show by name; porting code from System.Windows.MessageBox and forgetting to switch the call.

Common situations: Copy-paste from a WinForms / classic WPF sample that uses .Show(); code generation or a helper that calls Show on any Window-derived type; an older codebase being migrated to WPF UI's MessageBox.

Related errors


AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13). Data as JSON: /api/errors/a31dce17b7b21e33. Report an issue: GitHub.