lepoco/wpfui · error · InvalidOperationException
The DialogHost is not the same as the one that was previousl
Error message
The DialogHost is not the same as the one that was previously set.
What it means
Thrown by ShowAsync when the dialog carries its own host reference (dialog.DialogHostEx or dialog.DialogHost) that does not match the host registered on the service (ReferenceEquals fails). The service refuses to reparent a dialog whose host was pre-bound elsewhere, to prevent inconsistent rendering.
Source
Thrown at src/Wpf.Ui/ContentDialogService.cs:139
#pragma warning disable CS0618 // (Warning: Obsolete) To maintain compatibility
if (dialog == null)
{
throw new ArgumentNullException(nameof(dialog));
}
if (_dialogHostEx == null && _dialogHost == null)
{
throw new InvalidOperationException("The DialogHost was never set.");
}
object? svcHost = _dialogHostEx is not null ? _dialogHostEx : _dialogHost;
object? dlgHost = dialog.DialogHostEx is not null ? dialog.DialogHostEx : dialog.DialogHost;
if (dlgHost != null && !ReferenceEquals(dlgHost, svcHost))
{
throw new InvalidOperationException(
"The DialogHost is not the same as the one that was previously set."
);
}
if (_dialogHostEx != null)
{
dialog.DialogHostEx = _dialogHostEx;
}
else
{
dialog.DialogHost = _dialogHost;
}
return dialog.ShowAsync(cancellationToken);
#pragma warning restore CS0618 // (Warning: Obsolete)
}
}View on GitHub (pinned to ffebacd610)
Solutions
- Do not pre-set DialogHost/DialogHostEx on the ContentDialog; let the service assign it during ShowAsync.
- If the dialog must carry a host, ensure it is the exact same instance the service was configured with.
- Create a fresh ContentDialog per host context rather than reusing one across hosts.
Example fix
// before dialog.DialogHostEx = someOtherHost; await _dialogService.ShowAsync(dialog, ct); // mismatch // after // leave dialog.DialogHostEx unset; the service assigns its own host await _dialogService.ShowAsync(dialog, ct);
Defensive patterns
Strategy: validation
Validate before calling
if (dialog.DialogHostEx is not null && !ReferenceEquals(dialog.DialogHostEx, service.GetDialogHostEx())) { /* clear or realign before ShowAsync */ } Type guard
static bool DialogHostMatches(ContentDialog d, object svcHost) => d.DialogHostEx is null || ReferenceEquals(d.DialogHostEx, svcHost);
Prevention
- Do not pre-set DialogHost/DialogHostEx on the dialog; let the service assign it.
- Use a fresh ContentDialog per host context.
- Ensure the dialog's host reference equals the service's host when set explicitly.
When it happens
Trigger: A ContentDialog had its DialogHost/DialogHostEx explicitly set to a different host than the one the service holds, then ShowAsync is called. The mismatch check compares references and throws.
Common situations: Sharing a ContentDialog across windows/hosts, reusing a dialog instance after moving it to a different host, or explicitly setting DialogHost on the dialog in XAML while the service has its own host.
Related errors
- The DialogHost was never set.
- Cannot change DialogHost while the dialog is being shown.
- Cannot set ContentPresenter: a ContentDialogHost host has al
- Cannot set ContentDialogHost: a legacy ContentPresenter host
- Cannot set DialogHost when DialogHostEx is already set.
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/2e5f9ff6dd4228aa.
Report an issue: GitHub.