Looking at this forum a year later.
I believe having a service that can be injected to do this would be a great addition to syncfusion.
Looking at other blazor components, for example Radzen, they provide such a service and it works really well.
using Microsoft.AspNetCore.Components;
namespace BlazorSignalRApp.Components
{
public class ToastOptions
{
public string Title { get; set; }
public string Content { get; set; }
}
}
|
using System;
using Microsoft.AspNetCore.Components;
namespace BlazorSignalRApp.Components
{
public class ToastService
{
public event Action<ToastOptions> ToastInstance;
public void Open(ToastOptions options)
{
// Invoke ToastComponent to update and show the toast with messages
this.ToastInstance.Invoke(options);
}
}
}
|
@using Syncfusion.Blazor.Popups;
@using Syncfusion.Blazor.Notifications;
<SfToast @ref="ToastObj" Timeout=3000>
<ToastTemplates>
<Title>
@Options.Title
</Title>
<Content>
@Options.Content
</Content>
</ToastTemplates>
<ToastPosition X="Right"></ToastPosition>
</SfToast>
@code {
[Inject]
public ToastService ToastService { get; set; }
SfToast ToastObj;
private bool RenderToast { get; set; } = false;
// Parameter
private ToastOptions Options = new ToastOptions();
protected override async Task OnInitializedAsync()
{
// Update the parameter in local variable and render the toast
ToastService.ToastInstance += (ToastOptions options) =>
{
InvokeAsync(() =>
{
this.Options.Title = options.Title;
this.Options.Content = options.Content;
this.RenderToast = true;
this.StateHasChanged();
this.ToastObj.ShowAsync();
});
};
}
}
|
using BlazorSignalRApp.Components;
namespace BlazorSignalRApp
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<ToastService>();
...
}
}
}
|
@using BlazorSignalRApp.Components
…
…
<ToastComponent />
|
@page "/"
@using BlazorSignalRApp.Components
@inject ToastService ToastServices
<button class="e-btn" @onclick="@ShowToast1"> Show Toast</button>
@code {
private void ShowToast1()
{
this.ToastServices.Open(new ToastOptions()
{
Title = "Toast Title",
Content = "Toast content", // Dynamic Content
});
}
}
|
Thank you very much for the above sample.
I have downloaded the sample, every thing works fine, but when I make the CssClass as one the service options it dos not show the style as expected how ever when I click the second time to show the toast it applies the style and every things in ToastOptions show as expected but after the second click.
Thank you very much for your support.
@using BlazorSignalRApp.Components
@inject ToastService ToastServices
<button class="e-btn" @onclick="@ShowToast1"> Show Toast</button>
@code {
private async Task ShowToast1()
{
await Task.Delay(1);
this.ToastServices.Open(new ToastOptions()
{
Title = "Toast Title",
Content = "Toast content", // Dynamic Content
CssClass = "glow"
});
}
} |
Hi Vinitha,
I have download the provided solution for global toast message. Unfortunately, it does not work for me. Currently I am using .NET 6 with Blazor Server App. Can you please provide a updated version for .NET 6?
Any help would be highly appreciated.
Thanks & Regards,
Jay
Hi everyone,
I was trying to find a solution for this same issue and came across this forum.
I took a look at the solutions proposed (in special the latest one - Buvana's .Net 6 Toast Service), however I found out a solution which, IMHO, looks simpler (please not I'm rather new at Blazor).
Basically I created a component with a CascadingValue called AppToast:
<CascadingValue Value="this">
@ChildContent
<SfToast @ref="_toast" ShowProgressBar="true" ShowCloseButton="true" Width="350px"
Title="@_title" Content="@_contents" Timeout="@Timeout" CssClass="@_cssClass" Icon="@($"fa {_icon}")">
<ToastPosition X="Center" Y="Top" />
<ToastAnimationSettings>
<ToastShowAnimationSettings Effect="ToastEffect.SlideTopIn"></ToastShowAnimationSettings>
<ToastHideAnimationSettings Effect="ToastEffect.FadeOut"></ToastHideAnimationSettings>
</ToastAnimationSettings>
</SfToast>
</CascadingValue>
@code {
[Parameter]
public RenderFragment ChildContent { get; set; } = default!;
[Parameter]
public int Timeout { get; set; } = 5000;
private SfToast _toast = default!;
private string _title = default!;
private string _contents = default!;
private string _cssClass = default!;
private string _icon = default!;
public async Task ShowSuccessAsync(string message)
{
_title = "Success";
_cssClass = "e-toast-success";
_icon = "fa-circle-check";
await ShowAsync(message);
}
public async Task ShowInfoAsync(string message)
{
...
}
public async Task ShowWarningAsync(string message)
{
...
}
public async Task ShowErrorAsync(string message)
{
...
}
private async Task ShowAsync(string message)
{
_contents = message;
StateHasChanged();
await _toast.ShowAsync();
}
}
Then I just wrapped this component around my MainLayout Component:
<AppToast>
<div class="page">
...
<main class="main-content">
<article class="content px-4">
@Body
</article>
</main>
</div>
</AppToast>
With this setup, in each (Child) component I wish to invoke it, I simply capture the CascadingParameter and invoke the desired method (ShowSuccess, Warning, etc.):
[CascadingParameter]
private AppToast Toast { get; set; } = default!;
...
await Toast.ShowSuccessAsync("The operation succeeded.");
So far, this approach is working well for me.
Best regards,
Marco.
Hi Macro,
Thank you for sharing your alternate solution with us. Please let us know if you have any further assistance.
Regards,
Buvana S
Please note that your toast component is broken in the most recent releases:
20.1.0.57 and 20.1.0.58
It is impossible to trigger the toast when used as a cascade value or in the sample project you provided here:
Sample: https://www.syncfusion.com/downloads/support/forum/153145/ze/SfToast_as_Service-292090813
Hi Davy,
We have added breaking changes to the 20.1.47 version to add the script and style references. By default, IgnoreScriptIsolation property is enabled. In the below sample, we have enabled IgnoreScriptIsolation.
Startup.cs
services.AddSyncfusionBlazor(); |
So, you need to manually refer to the script and styles file like below.
Note: You need to install Syncfusion.Blazor.Themes nuget package for referring styles.
_Host.cshtml
<link rel='nofollow' href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" /> <script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script> |
Documentation: https://blazor.syncfusion.com/documentation/common/adding-script-references
Release Notes: https://blazor.syncfusion.com/documentation/release-notes/20.1.47?type=all#breaking-changes
We have upgraded the sample to the 20.1.0.58 version with the above breaking changes. Can you please find the below sample?
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/SFB7C0~1-577141057
Please let us know if you have any concerns.
Regards,
Buvana S