lepoco/wpfui · error · InvalidOperationException
Cannot set ContentPresenter: a ContentDialogHost host has al
Error message
Cannot set ContentPresenter: a ContentDialogHost host has already been set. Only one host type is allowed per instance for compatibility.
What it means
Thrown by ContentDialogService.SetDialogHost(ContentPresenter) when the new-style host (_dialogHostEx of type ContentDialogHost) has already been set. The service forbids mixing legacy ContentPresenter hosting with the new ContentDialogHost to keep rendering/behavior consistent within one instance.
Source
Thrown at src/Wpf.Ui/ContentDialogService.cs:61
[Obsolete("Use GetDialogHost instead.")]
public ContentPresenter? GetContentPresenter()
{
return GetDialogHost();
}
/// <inheritdoc/>
[Obsolete("Use SetDialogHost(ContentDialogHost) instead.")]
public void SetDialogHost(ContentPresenter contentPresenter)
{
if (contentPresenter == null)
{
throw new ArgumentNullException(nameof(contentPresenter));
}
if (_dialogHostEx != null)
{
throw new InvalidOperationException(
"Cannot set ContentPresenter: a ContentDialogHost host has already been set. "
+ "Only one host type is allowed per instance for compatibility."
);
}
_dialogHost = contentPresenter;
}
/// <inheritdoc/>
[Obsolete("Use GetDialogHostEx() instead.")]
public ContentPresenter? GetDialogHost()
{
return _dialogHost;
}
/// <inheritdoc/>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="dialogHost"/> is <see langword="null"/>.View on GitHub (pinned to ffebacd610)
Solutions
- Pick one hosting model per service instance: use SetDialogHost(ContentDialogHost) and remove all legacy SetDialogHost(ContentPresenter) calls.
- Search the codebase for the obsolete SetDialogHost(ContentPresenter) usages (CS0618 warnings) and migrate them.
- If both are genuinely needed, use two separate ContentDialogService instances rather than mixing hosts on one.
Example fix
// before contentDialogService.SetDialogHost(myContentPresenter); // _dialogHostEx already set // after contentDialogService.SetDialogHost(myContentDialogHost);
Defensive patterns
Strategy: validation
Validate before calling
if (_dialogService.GetDialogHostEx() is null) { _dialogService.SetDialogHost(contentPresenter); } Type guard
static bool HostIsUnset(IContentDialogService svc) => svc.GetDialogHost() is null && svc.GetDialogHostEx() is null;
Prevention
- Adopt ContentDialogHost exclusively and remove obsolete SetDialogHost(ContentPresenter) calls.
- Resolve CS0618 warnings to catch lingering legacy host usage.
- Use one service instance per host type.
When it happens
Trigger: Calling the obsolete SetDialogHost(ContentPresenter) after SetDialogHost(ContentDialogHost) was already called on the same service instance. The field _dialogHostEx is non-null, triggering the InvalidOperationException.
Common situations: Migrating an app to the new ContentDialogHost API while leftover XAML/code still calls the legacy SetDialogHost(ContentPresenter). Or DI wiring that registers both host setups for the same singleton service.
Related errors
- Cannot set ContentDialogHost: a legacy ContentPresenter host
- 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/232ef111ce663623.
Report an issue: GitHub.