{"record":{"id":"1f23fc0e5569436d","repo":"lepoco/wpfui","slug":"the-snackbarpresenter-was-never-set","errorCode":null,"errorMessage":"The SnackbarPresenter was never set","messagePattern":"The SnackbarPresenter was never set","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/SnackbarService.cs","lineNumber":45,"sourceCode":"\n    /// <inheritdoc />\n    public SnackbarPresenter? GetSnackbarPresenter()\n    {\n        return _presenter;\n    }\n\n    /// <inheritdoc />\n    public void Show(\n        string title,\n        string message,\n        ControlAppearance appearance,\n        IconElement? icon,\n        TimeSpan timeout\n    )\n    {\n        if (_presenter is null)\n        {\n            throw new InvalidOperationException($\"The SnackbarPresenter was never set\");\n        }\n\n        _snackbar ??= new Snackbar(_presenter);\n\n        _snackbar.SetCurrentValue(Snackbar.TitleProperty, title);\n        _snackbar.SetCurrentValue(System.Windows.Controls.ContentControl.ContentProperty, message);\n        _snackbar.SetCurrentValue(Snackbar.AppearanceProperty, appearance);\n        _snackbar.SetCurrentValue(Snackbar.IconProperty, icon);\n        _snackbar.SetCurrentValue(\n            Snackbar.TimeoutProperty,\n            timeout.TotalSeconds == 0 ? DefaultTimeOut : timeout\n        );\n\n        _snackbar.Show(true);\n    }\n}\n","sourceCodeStart":27,"sourceCodeEnd":62,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/SnackbarService.cs#L27-L62","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add a <ui:SnackbarPresenter x:Name=\"SnackbarPresenter\" /> to your main window.","In the window's Loaded handler call snackbarService.SetSnackbarPresenter(snackbarPresenter).","Defer snackbarService.Show calls until after the presenter has been set."],"exampleFix":"// XAML: <ui:SnackbarPresenter x:Name=\"RootSnackbar\" />\n\n// before\ncontainer.GetRequiredService<ISnackbarService>().Show(\"Hi\", \"msg\", ...); // throws\n\n// after\nprivate void OnLoaded(object sender, RoutedEventArgs e)\n{\n    var svc = _host.Services.GetRequiredService<ISnackbarService>();\n    svc.SetSnackbarPresenter(RootSnackbar);\n}","handlingStrategy":"validation","validationCode":"if (snackbarService.GetSnackbarPresenter() is null)\n{\n    throw new InvalidOperationException(\"Call SetSnackbarPresenter before showing a snackbar.\");\n}","typeGuard":"static bool IsPresenterReady(ISnackbarService svc) => svc.GetSnackbarPresenter() is not null;","tryCatchPattern":"try { snackbarService.Show(title, message, appearance, icon, timeout); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"SnackbarPresenter was never set\"))\n{\n    _logger.LogError(ex, \"SnackbarPresenter not configured; cannot show snackbar.\");\n}","preventionTips":["Place a SnackbarPresenter in the main window XAML.","Call SetSnackbarPresenter in the window Loaded handler before any Show call.","Gate view-model Show calls behind a 'service ready' flag."],"tags":["snackbar","initialization","service","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}