Toast in .Net MAUI

.NET MAUI is a powerful platform for building cross-platform mobile applications, and with the right tools and resources, it's easy to implement Toast in your App. This tutorial will walk you through the steps for adding Avatar View in .NET MAUI using .NET MAUI Community Toolkit.

Project Setup

Implementation

.NET MAUI Community Toolkit is the key to achieving drawing in our App is to use Community.ToolKit.Maui NuGet package. It is a collection of reusable elements such as animations, and behaviors converters, among others, for developing applications for iOS, Android, macOS, and WinUI using MAUI.

Install .NET MAUI Community Toolkit

var builder = MauiApp.CreateBuilder();
builder
	.UseMauiApp<App>()
	.UseMauiCommunityToolkit()
	.ConfigureFonts(fonts =>
	{
		fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
		fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
	});
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        await Toast.Make("Howdy, I'm a Toast!",
                  ToastDuration.Long,
                  16)
            .Show(cancellationTokenSource.Token);

Make() and Show() are two crucial methods that must be used to display the Toast on the screen. Each of these methods has particular attributes that aid with Toast setting. Let's take a closer look at them.

Make

Show

Full Code

using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
namespace MauiToast;
public partial class MainPage : ContentPage
{
	int count = 0;
	public MainPage()
	{
		InitializeComponent();
	}
	private async void OnCounterClicked(object sender, EventArgs e)
	{
        CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        await Toast.Make("Howdy, I'm a Toast!",
                  ToastDuration.Long,
                  16)
            .Show(cancellationTokenSource.Token);
    }
}

Demo

Toast in .Net MAUI

Toast in .Net MAUI

Download Code

You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article and it is helpful to you, do like, share the article & star the repository on GitHub.