Toast is a timed alert that appears at the bottom of the screen. It is automatically dismissed after a configurable duration of time.
It provides simple feedback to the user about an operation in a small alert.
To access the Toast functionality, the following platform specific setup is required.
When using Snackbar it is essential to perform the following two steps:
1. Enable the snackbar usage with the MauiAppBuilder
When using the UseMauiCommunityToolkit make use of the options parameter to enable the snackbar usage on Windows as follows:
var builder = MauiApp.CreateBuilder()
.UseMauiCommunityToolkit(options =>
{
options.SetShouldEnableSnackbarOnWindows(true);
})
The above will automatically register the required handlers by configuring lifecycle events (OnLaunched and OnClosed).
2. Include ToastNotification registrations in your Package.appxmanifest file
To handle the snackbar actions you will need to modify the Platform\Windows\Package.appxmanifest file as follows:
- In Package.appxmanifest, in the opening
<Package> tag, add the following XML Namespaces:
xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
- In Package.appxmanifest, also in the opening
<Package> tag, update IgnorableNamespaces to include uap rescap com and desktop:
IgnorableNamespaces="uap rescap com desktop"
Example: Completed <Package> Tag
Here is an example of a completed opening <Package> tag that has added support for Snackbar:
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
IgnorableNamespaces="uap rescap com desktop">
- In Package.appxmanifest, inside of each
<Application> tag, add the following extensions:
<Extensions>
<!-- Specify which CLSID to activate when notification is clicked -->
<desktop:Extension Category="windows.toastNotificationActivation">
<desktop:ToastNotificationActivation ToastActivatorCLSID="6e919706-2634-4d97-a93c-2213b2acc334" />
</desktop:Extension>
<!-- Register COM CLSID -->
<com:Extension Category="windows.comServer">
<com:ComServer>
<com:ExeServer Executable="YOUR-PATH-TO-EXECUTABLE" DisplayName="$targetnametoken$" Arguments="----AppNotificationActivated:"> <!-- Example path to executable: CommunityToolkit.Maui.Sample\CommunityToolkit.Maui.Sample.exe -->
<com:Class Id="6e919706-2634-4d97-a93c-2213b2acc334" />
</com:ExeServer>
</com:ComServer>
</com:Extension>
</Extensions>
Example: Completed <Applications> tag
Here is an example of a completed <Applications> tag that now has added support for Snackbar:
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="$targetentrypoint$">
<uap:VisualElements
DisplayName="$placeholder$"
Description="$placeholder$"
Square150x150Logo="$placeholder$.png"
Square44x44Logo="$placeholder$.png"
BackgroundColor="transparent">
<uap:DefaultTile Square71x71Logo="$placeholder$.png" Wide310x150Logo="$placeholder$.png" Square310x310Logo="$placeholder$.png" />
<uap:SplashScreen Image="$placeholder$.png" />
</uap:VisualElements>
<Extensions>
<desktop:Extension Category="windows.toastNotificationActivation">
<desktop:ToastNotificationActivation ToastActivatorCLSID="6e919706-2634-4d97-a93c-2213b2acc334" />
</desktop:Extension>
<com:Extension Category="windows.comServer">
<com:ComServer>
<com:ExeServer Executable="YOUR-PATH-TO-EXECUTABLE" DisplayName="$targetnametoken$" Arguments="----AppNotificationActivated:"> <!-- Example path to executable: CommunityToolkit.Maui.Sample\CommunityToolkit.Maui.Sample.exe -->
<com:Class Id="6e919706-2634-4d97-a93c-2213b2acc334" />
</com:ExeServer>
</com:ComServer>
</com:Extension>
</Extensions>
</Application>
</Applications>
Example: Updated Package.appxmanifest File to Support Snackbar
Below is an example Package.appxmanifest file that has been updated to support Snackbar on Windows:
<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
IgnorableNamespaces="uap rescap com desktop">
<Identity Name="maui-package-name-placeholder" Publisher="CN=Microsoft" Version="0.0.0.0" />
<Properties>
<DisplayName>$placeholder$</DisplayName>
<PublisherDisplayName>Microsoft</PublisherDisplayName>
<Logo>$placeholder$.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
</Dependencies>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="$targetentrypoint$">
<uap:VisualElements
DisplayName="$placeholder$"
Description="$placeholder$"
Square150x150Logo="$placeholder$.png"
Square44x44Logo="$placeholder$.png"
BackgroundColor="transparent">
<uap:DefaultTile Square71x71Logo="$placeholder$.png" Wide310x150Logo="$placeholder$.png" Square310x310Logo="$placeholder$.png" />
<uap:SplashScreen Image="$placeholder$.png" />
</uap:VisualElements>
<Extensions>
<desktop:Extension Category="windows.toastNotificationActivation">
<desktop:ToastNotificationActivation ToastActivatorCLSID="6e919706-2634-4d97-a93c-2213b2acc334" />
</desktop:Extension>
<com:Extension Category="windows.comServer">
<com:ComServer>
<com:ExeServer Executable="YOUR-PATH-TO-EXECUTABLE" DisplayName="$targetnametoken$" Arguments="----AppNotificationActivated:"> <!-- Example path to executable: CommunityToolkit.Maui.Sample\CommunityToolkit.Maui.Sample.exe -->
<com:Class Id="6e919706-2634-4d97-a93c-2213b2acc334" />
</com:ExeServer>
</com:ComServer>
</com:Extension>
</Extensions>
</Application>
</Applications>
<Capabilities>
<rescap:Capability Name="runFullTrust" />
</Capabilities>
</Package>
For more information on handling activation: Send a local toast notification from C# apps
Syntax
C#
To display Toast, first create it using the static method Toast.Make(), then display it using its method Show().
using CommunityToolkit.Maui.Alerts;
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
string text = "This is a Toast";
ToastDuration duration = ToastDuration.Short;
double fontSize = 14;
var toast = Toast.Make(text, duration, fontSize);
await toast.Show(cancellationTokenSource.Token);
When calling Toast.Make(), its parameter string text is required. All other parameters are optional. Its optional parameter ToastDuration duration uses the default duration of ToastDuration.Short. Its optional parameter double fontSize uses the default value of 14.0.
The following screenshot shows the resulting Toast:
Properties
| Property |
Type |
Description |
Default value |
| Text |
string |
Text that displayed in the Toast. |
Required |
| Duration |
ToastDuration |
Duration Toast displayed. |
ToastDuration.Short |
| TextSize |
double |
Text font size. |
14.0 |
ToastDuration
The ToastDuration enumeration defines the following members:
Short - Display Toast for 2 seconds
Long - Display Toast for 3.5 seconds
These values adhere to the constants defined in the android.widget.Toast API.
Methods
| Method |
Description |
| Show |
Display the requested Toast. If a Toast is currently displayed, it will automatically be dismissed before the requested Toast is displayed. |
| Dismiss |
Dismiss the current toast. |
Note
You can display only one Toast at a time. If you call the Show method a second time, the first Toast will automatically be dismissed.
Examples
You can find an example of this feature in action in the .NET MAUI Community Toolkit Sample Application.
API
You can find the source code for Toast over on the .NET MAUI Community Toolkit GitHub repository.
- The API allows override existing methods with your own implementation or creating your own Toast, by implementing
IToast interface.
- Toast is implemented on Android, created by Google. Other platforms use a custom-implemented container (
UIView for iOS and MacCatalyst, ToastNotification on Windows).
- Toast on Tizen can't be customized with its
Duration and TextSize properties.