lepoco/wpfui · error · InvalidOperationException

The SnackbarPresenter was never set

Error message

The SnackbarPresenter was never set

What it means

SnackbarService.Show requires a SnackbarPresenter (a content host placed in the visual tree) to display snackbars. Show throws InvalidOperationException if SetSnackbarPresenter was never called, because it needs the presenter to construct and route the Snackbar. The service deliberately fails fast rather than silently dropping the message.

Source

Thrown at src/Wpf.Ui/SnackbarService.cs:45

    /// <inheritdoc />
    public SnackbarPresenter? GetSnackbarPresenter()
    {
        return _presenter;
    }

    /// <inheritdoc />
    public void Show(
        string title,
        string message,
        ControlAppearance appearance,
        IconElement? icon,
        TimeSpan timeout
    )
    {
        if (_presenter is null)
        {
            throw new InvalidOperationException($"The SnackbarPresenter was never set");
        }

        _snackbar ??= new Snackbar(_presenter);

        _snackbar.SetCurrentValue(Snackbar.TitleProperty, title);
        _snackbar.SetCurrentValue(System.Windows.Controls.ContentControl.ContentProperty, message);
        _snackbar.SetCurrentValue(Snackbar.AppearanceProperty, appearance);
        _snackbar.SetCurrentValue(Snackbar.IconProperty, icon);
        _snackbar.SetCurrentValue(
            Snackbar.TimeoutProperty,
            timeout.TotalSeconds == 0 ? DefaultTimeOut : timeout
        );

        _snackbar.Show(true);
    }
}

View on GitHub (pinned to ffebacd610)

Solutions

  1. Add a <ui:SnackbarPresenter x:Name="SnackbarPresenter" /> to your main window.
  2. In the window's Loaded handler call snackbarService.SetSnackbarPresenter(snackbarPresenter).
  3. Defer snackbarService.Show calls until after the presenter has been set.

Example fix

// XAML: <ui:SnackbarPresenter x:Name="RootSnackbar" />

// before
container.GetRequiredService<ISnackbarService>().Show("Hi", "msg", ...); // throws

// after
private void OnLoaded(object sender, RoutedEventArgs e)
{
    var svc = _host.Services.GetRequiredService<ISnackbarService>();
    svc.SetSnackbarPresenter(RootSnackbar);
}
Defensive patterns

Strategy: validation

Validate before calling

if (snackbarService.GetSnackbarPresenter() is null)
{
    throw new InvalidOperationException("Call SetSnackbarPresenter before showing a snackbar.");
}

Type guard

static bool IsPresenterReady(ISnackbarService svc) => svc.GetSnackbarPresenter() is not null;

Try / catch

try { snackbarService.Show(title, message, appearance, icon, timeout); }
catch (InvalidOperationException ex) when (ex.Message.Contains("SnackbarPresenter was never set"))
{
    _logger.LogError(ex, "SnackbarPresenter not configured; cannot show snackbar.");
}

Prevention

When it happens

Trigger: Calling snackbarService.Show(...) before calling snackbarService.SetSnackbarPresenter(...); the presenter host was never added to the window's XAML; the service was constructed but never wired to a presenter.

Common situations: Initialising ISnackbarService via the host/container but forgetting to place a SnackbarPresenter in the main window and call SetSnackbarPresenter in the window's Loaded handler; injecting the service into a view-model and calling Show at startup before the window is ready.

Related errors


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