blazor toast in csharp

To display a toast notification in a Blazor application using C#, you can use the BlazorToast library, which can be installed via NuGet. Here are the steps to set up and use BlazorToast:

  1. Install the BlazorToast package via NuGet:
main.cs
Install-Package BlazorToast
28 chars
2 lines
  1. Register BlazorToast in the Startup.cs file:
main.cs
using BlazorToast;

public void ConfigureServices(IServiceCollection services)
{
    services.AddBlazorToast();
}
114 chars
7 lines
  1. In the component where you want to display the toast, inject the IToastService:
main.cs
@using BlazorToast
@inject IToastService ToastService

<button @onclick="ShowToast">Show Toast</button>
104 chars
5 lines
  1. Create a method to call the ShowToast method of the IToastService, passing in the message and options for the toast:
main.cs
private void ShowToast()
{
    ToastService.ShowToast("This is a toast message", new ToastOption()
    {
        Category = ToastCategory.Success,
        Position = ToastPosition.TopCenter,
        TimeOut = 3000
    });
}
224 chars
10 lines

This will display a green success toast message for 3 seconds at the top center of the screen.

You can customize the toast further by changing the options, such as the category (success, info, warning, error), position (top/left/center/bottom), and timeout (in milliseconds).

gistlibby LogSnag