I’m working with a .NET MAUI application on Windows base application, where I’ve implemented Toasts for notifications. However, I’m encountering an issue where clicking on the toast message (or Snackbar) opens a new instance of the application, even though the app is already running. This behavior is disruptive, and I would prefer that clicking on the notification simply brings the existing app window to the foreground rather than launching a duplicate instance.
Environment :
Platform: Windows
Framework: .NET MAUI
CommunityToolkit.Maui- Version: [7.0.1]
If anyone has encountered this issue or knows how to ensure that clicking on a .NET MAUI Toast or Snackbar does not create a new instance, I would greatly appreciate any guidance or workaround suggestions.
I would prefer that clicking on the notification simply brings the existing app window to the foreground rather than launching a duplicate instance.
Below is the code for reference:
using CommunityToolkit.Maui.Alerts;
private async void OnCounterClicked(object sender, EventArgs e)
{
await Toast.Make("Toast is here", ToastDuration.Short).Show();
}
0
Yes, it is just the case as you said.
But you can try the solution. Please add the following code to file
App.xaml.cs
of Windows
platform for your Maui app:
public App()
{
var singleInstance = AppInstance.FindOrRegisterForKey("SingleInstanceApp");
if (!singleInstance.IsCurrent)
{
// this is another instance
// 1. activate the first instance
var currentInstance = AppInstance.GetCurrent();
var args = currentInstance.GetActivatedEventArgs();
singleInstance.RedirectActivationToAsync(args).GetAwaiter().GetResult();
// 2. close this instance
Process.GetCurrentProcess().Kill();
return;
}
// this is the first instance
// 1. register for future activation
singleInstance.Activated += OnAppInstanceActivated;
// 2. continue with normal startup
this.InitializeComponent();
}
private void OnAppInstanceActivated(object? sender, AppActivationArguments e)
{
// handle the old app being loaded
}