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

  1. Pick one hosting model per service instance: use SetDialogHost(ContentDialogHost) and remove all legacy SetDialogHost(ContentPresenter) calls.
  2. Search the codebase for the obsolete SetDialogHost(ContentPresenter) usages (CS0618 warnings) and migrate them.
  3. 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

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


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