Hi guys, this is how you can generate toast notifications on .Net Maui using the maui Community Toolkit.
- First step we need the following NuGet Packages
- Firstly this is what the code looks like to generate a toast notification
using CommunityToolkit.Maui.Alerts; var toast = Toast.Make(message, duration, fontSize); await toast.Show(cancellationToken);
However I feel to make the code reusable It would be best to create a seperate class, in future we can add the snackbar and other dialogs to the same class. The Toast Duration and the textSize for toast notification is optional, so I made use of the using System.Runtime.InteropServices
.In my case I have called it MessageHelper.cs. This is what it looks like.
public async Task GenerateToast(string message, [Optional] ToastDuration duration, [Optional] double textSize)
{
var toast = CommunityToolkit.Maui.Alerts.Toast.Make(message);
await toast.Show();
}
now everytime I need a toast notification I only need to create the MessageHelper object and call the method.
MessageHelper toast = new MessageHelper();
private async void ToastButton_Clicked(object sender, EventArgs e)
{
await toast.GenerateToast("Hello World");
}
... and that's all you need, obviously you can refactor this code if you have a better implementation such as mvvm.