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
- Replace messageBox.Show() with await messageBox.ShowDialogAsync(showAsDialog: false).
- For a non-modal display, pass showAsDialog: false to ShowDialogAsync.
- Search the codebase for '.Show()' on MessageBox instances and update each.
- 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
- Treat the [Obsolete] warning on Show() as a compile-time blocker.
- Grep for '.Show()' over MessageBox instances during migration.
- Update generic Window helpers to special-case WPF.UI MessageBox.
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
- Use {nameof(Close)} with MessageBoxResult instead
- Unable to find the base directory of the application.
- Unable to determine the window source.
- Cannot set ContentPresenter: a ContentDialogHost host has al
- Cannot set ContentDialogHost: a legacy ContentPresenter host
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/a31dce17b7b21e33.
Report an issue: GitHub.