.NET MAUI, a powerful platform for developing cross-platform mobile applications, provides a seamless way to incorporate Toast functionality into your app. With the aid of the appropriate tools and resources, implementing an Toast in .NET MAUI using the .NET MAUI Community Toolkit is a straightforward process. In this tutorial, we will guide you through the necessary steps to successfully integrate Toast into your .NET MAUI application.
Quick Links:
Project Setup:
- Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
- In the Create a new project window, select MAUI in the All project types drop-down, select the .NET MAUI App template, and click the Next button:
- In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
- In the Additional information window, click the Create button:
- Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app
Implementation:
.NET MAUI Community Toolkit is the key to achieve drawing in our App is to use Community.ToolKit.Maui NuGet package. It is a collection of reusable elements such as animations, behaviors converters, among others, for developing applications for iOS, Android, macOS and WinUI using MAUI.
Install .NET MAUI Community Toolkit
- Install .NET MAUI Community Toolkit nuGet package on your .NET MAUI application.
- Now initialize the plugin. Go to your MauiProgram.cs file. In the CreateMauiApp method, place in the .UseMauiApp<App>() line and just below it add the following line of code.
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("Hi, How are you?",
ToastDuration.Long,
16)
.Show(cancellationTokenSource.Token);
Make:
The Make() method is responsible for creating the Toast and requires the following parameters:- Text: This is a mandatory parameter represents the message that will be displayed in the Toast.
- Duration: This parameter specifies the exact duration for which the Toast message will be shown on the screen before fading away. It is an optional parameter and it's default ToastDuration type is ToastDuration.Short:
- TextSize: This parameter is an optional paramter to set the text size of the Toast. Default value of this parameter is 14 and it's type is double.
Show:
This method is responsible for displaying the requested Toast message on the screen.Full Code:
Demo
Download Code:
The idea behind this tutorial is from "AskXammy"
0 comments:
Please Comment about the Posts and Blog