Getting Started with Blazor Toast Component
16 Oct 20255 minutes to read
This section explains how to add the Blazor Toast component to a Blazor WebAssembly app using Visual Studio or Visual Studio Code.
A quick-start video and a sample repository are available:
- Video:
- Sample on GitHub
Prerequisites
Create a new Blazor App in Visual Studio
Create a Blazor WebAssembly app using Visual Studio via Microsoft templates or the Syncfusion® Blazor extension.
Install Syncfusion® Blazor Notifications and Themes NuGet in the App
Install Syncfusion.Blazor.Notifications and Syncfusion.Blazor.Themes using the NuGet Package Manager (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), or run the following commands:
Install-Package Syncfusion.Blazor.Notifications -Version 31.2.2
Install-Package Syncfusion.Blazor.Themes -Version 31.2.2
NOTE
Syncfusion® Blazor components are available on nuget.org. Refer to the NuGet packages topic for the list of available packages and component details.
Prerequisites
Create a new Blazor App in Visual Studio Code
Create a Blazor WebAssembly app using Visual Studio Code via Microsoft templates or the Syncfusion® Blazor extension.
Alternatively, create a WebAssembly application using the following commands in the integrated terminal:
dotnet new blazorwasm -o BlazorApp
cd BlazorApp
Install Syncfusion® Blazor Notifications and Themes NuGet in the App
Use the integrated terminal in Visual Studio Code from the project root (where the .csproj file is located), then run:
dotnet add package Syncfusion.Blazor.Notifications -v 31.2.2
dotnet add package Syncfusion.Blazor.Themes -v 31.2.2
dotnet restore
NOTE
Syncfusion® Blazor components are available on nuget.org. Refer to the NuGet packages topic for the list of available packages and component details.
Register Syncfusion® Blazor Service
Open ~/_Imports.razor and import the Syncfusion.Blazor and Syncfusion.Blazor.Notifications namespaces:
@using Syncfusion.Blazor
@using Syncfusion.Blazor.NotificationsRegister the Syncfusion® Blazor service in ~/Program.cs of the Blazor WebAssembly app:
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Syncfusion.Blazor;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddSyncfusionBlazor();
await builder.Build().RunAsync();Add stylesheet and script resources
The theme stylesheet and script are provided via NuGet as Static Web Assets. Include the stylesheet and script references in the <head> section of ~/wwwroot/index.html:
<head>
<!-- ... -->
<link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" />
<script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>
</head>NOTE
See the Blazor Themes topic to discover options for referencing themes (Static Web Assets, CDN, and CRG). Also, see Adding Script Reference for different approaches to add script references in a Blazor application.
Add Blazor Toast component
Add the Syncfusion® Blazor Toast component to ~/Pages/Index.razor:
<div class="col-lg-12 control-section toast-default-section">
<SfToast ID="toast_default" @ref="ToastObj" Title="Adaptive Tiles Meeting" Content="@ToastContent" Timeout="5000" Icon="e-meeting">
<ToastPosition X="@ToastPosition"></ToastPosition>
</SfToast>
<div class="col-lg-12 col-sm-12 col-md-12 center">
<div id="toastBtnDefault" style="margin: auto; text-align: center">
<button class="e-btn" @onclick="@ShowOnClick">Show Toasts</button>
<button class="e-btn" @onclick="@HideOnClick">Hide All</button>
</div>
</div>
</div>
<style>
#toast_default .e-meeting::before {
content: "\e705";
font-size: 17px;
}
.bootstrap4 #toast_default .e-meeting::before {
content: "\e763";
font-size: 20px;
}
</style>
@code {
private SfToast ToastObj;
private string ToastPosition = "Right";
private string ToastContent = "Conference Room 01 / Building 135 10:00 AM-10:30 AM";
private async Task ShowOnClick()
{
await ToastObj.ShowAsync();
}
private async Task HideOnClick()
{
await ToastObj.HideAsync("All");
}
}Launch the application with Ctrl+F5 (Windows) or ⌘+F5 (macOS). The Syncfusion® Blazor Toast component renders in the default web browser.
See Also
- Show or hide toast from any page using service in Blazor
- Getting Started with Syncfusion® Blazor WebAssembly App in Visual Studio or .NET CLI
- Getting Started with Syncfusion® Blazor Web App in Visual Studio or .NET CLI
NOTE
An interactive Blazor Toast example demonstrates how to render and configure the Toast component.