I am using the MAUI Community Toolkit Snackbar in my MAUI Project using MVVM (using Prism).
To make my life easier I have a “generic” class that abstracts some of the setup of my Snackbar. This helps simplify the call to ShowSuccess… ShowWarning.. ShowError etc.
public class NotificationService
{
public async Task ShowSuccessToastAsync(string messageText)
{
_logger.Debug($"ShowSuccessToastAsync with Message:{messageText}");
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
var snackbarOptions = new SnackbarOptions
{
BackgroundColor = Color.FromArgb("#71b068"),
TextColor = Colors.Black,
ActionButtonTextColor = Colors.Yellow,
CornerRadius = new CornerRadius(10),
Font = Font.SystemFontOfSize(14),
ActionButtonFont = Font.SystemFontOfSize(14),
};
TimeSpan duration = TimeSpan.FromSeconds(3);
var snackbar = Snackbar.Make(messageText, null, "", duration, snackbarOptions);
await snackbar.Show(cancellationTokenSource.Token);
}
}
Then, from my ViewModels I can Inject the NotificationService and display the SnackBar using something like;
_ = _notificationService.ShowSuccessToastAsync("Logs uploaded Successfully");
This works fine on the first page. However for any subesquent page that gets navigated to, the Snackbar doesn’t show.
I assume there is something with the anchor not being set in the Snackbar.Make() call meaning that it is probably showing on the inital page which is below the current page on the navigation stack however I don’t actually know how to pass the current view to it if call the snackbar.Show from the ViewModel.
How can I get the snackbar to display on the current view?
2