This is a JavaScript free toast implementation for Blazor and Razor Components applications.
There is currently an issue with Blazor server-side apps (not Blazor). They are unable to import static assets from component libraries such as this one.
You can still use this package, however, you will need to manually add the CSS to your apps wwwroot
folder. You will then need to add a reference to it in the head
tag of your apps index.html
page.
Alternatively, there is a great package by Mister Magoo which offers a solution to this problem without having to manually copy files.
You can install the package via the nuget package manager just search for Blazored.Toast. You can also install via powershell using the following command.
Install-Package Blazored.Toast
Or via the dotnet CLI.
dotnet add package Blazored.Toast
First, you will need to register the Blazored Toast service in your applications Startup.ConfigureServices
method.
public void ConfigureServices(IServiceCollection services)
{
services.AddBlazoredToast(options => {
options.Timeout = 10; // default: 5
options.Position = ToastPosition.BottomRight; // default: ToastPosition.TopRight
});
}
Second, add the following to your _Imports.razor
@using Blazored
@using Blazored.Toast.Services
Third and finally you will need to register the <BlazoredToasts />
component in your applications MainLayout.cshtml.
In order to show a toast you have to inject the IToastService
into the component or service you want to trigger a toast. You can then call one of the following methods depending on what kind of toast you want to display, passing in a message and an optional heading.
ShowInfo
ShowSuccess
ShowWarning
ShowError
@page "/toastdemo"
@inject IToastService toastService
<h1>Toast Demo</h1>
To show a toast just click one of the buttons below.
<button class="btn btn-info" onclick="@(() => toastService.ShowInfo("I'm an INFO message"))">Info Toast</button>
<button class="btn btn-success" onclick="@(() => toastService.ShowSuccess("I'm a SUCCESS message with a custom title", "Congratulations!"))">Success Toast</button>
<button class="btn btn-warning" onclick="@(() => toastService.ShowWarning("I'm a WARNING message"))">Warning Toast</button>
<button class="btn btn-danger" onclick="@(() => toastService.ShowError("I'm an ERROR message"))">Error Toast</button>