lepoco/wpfui · error · InvalidOperationException
Cannot set ContentDialogHost: a legacy ContentPresenter host
Error message
Cannot set ContentDialogHost: a legacy ContentPresenter host has already been set. Only one host type is allowed per instance for compatibility.
What it means
Thrown by ContentDialogService.SetDialogHost(ContentDialogHost) when the legacy host (_dialogHost of type ContentPresenter) is already set. This is the symmetric guard to error 11: once a ContentPresenter host is in use, switching to ContentDialogHost on the same instance is rejected to avoid mixed hosting.
Source
Thrown at src/Wpf.Ui/ContentDialogService.cs:103
/// <remarks>
/// <para>
/// This method sets the enhanced <see cref="ContentDialogHost"/> to contain and manage dialogs.
/// For compatibility reasons, an instance can have either a legacy host (set via
/// <see cref="SetDialogHost(ContentPresenter)"/>) or an enhanced host (set via this method),
/// but not both.
/// </para>
/// </remarks>
public void SetDialogHost(ContentDialogHost dialogHost)
{
if (dialogHost == null)
{
throw new ArgumentNullException(nameof(dialogHost));
}
// Defense mechanism: prevent mixed host types for compatibility
if (_dialogHost != null)
{
throw new InvalidOperationException(
"Cannot set ContentDialogHost: a legacy ContentPresenter host has already been set. "
+ "Only one host type is allowed per instance for compatibility."
);
}
_dialogHostEx = dialogHost;
}
/// <inheritdoc/>
public ContentDialogHost? GetDialogHostEx()
{
return _dialogHostEx;
}
/// <inheritdoc/>
public Task<ContentDialogResult> ShowAsync(ContentDialog dialog, CancellationToken cancellationToken)
{
#pragma warning disable CS0618 // (Warning: Obsolete) To maintain compatibilityView on GitHub (pinned to ffebacd610)
Solutions
- Standardize on ContentDialogHost for the instance and avoid the legacy ContentPresenter host entirely.
- If mid-migration, ensure only one SetDialogHost call runs per service instance; guard with a flag or restructure DI registration.
- Use distinct ContentDialogService instances when both host styles must coexist in the app.
Example fix
// before contentDialogService.SetDialogHost(myContentDialogHost); // _dialogHost already set // after // remove the earlier legacy call and use only: contentDialogService.SetDialogHost(myContentDialogHost);
Defensive patterns
Strategy: validation
Validate before calling
if (_dialogService.GetDialogHost() is null) { _dialogService.SetDialogHost(dialogHost); } Type guard
static bool LegacyHostUnset(IContentDialogService svc) => svc.GetDialogHost() is null;
Prevention
- Avoid mixing legacy and new hosts on a single service instance.
- Centralize host setup in one initialization path.
- Migrate all legacy ContentPresenter host code to ContentDialogHost.
When it happens
Trigger: Calling SetDialogHost(ContentDialogHost) after the obsolete SetDialogHost(ContentPresenter) was already invoked. The field _dialogHost is non-null, so the InvalidOperationException fires.
Common situations: App initializes with the legacy ContentPresenter host (from older tutorials) and then attempts to upgrade to ContentDialogHost without clearing the prior host, or two different code paths each set a host type.
Related errors
- Cannot set ContentPresenter: a ContentDialogHost host has al
- Cannot set DialogHost when DialogHostEx is already set.
- Cannot set DialogHostEx when DialogHost is already set.
- The DialogHost was never set.
- The DialogHost is not the same as the one that was previousl
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/b3d9e2211649ec9d.
Report an issue: GitHub.