|
<CascadingValue Value="@this">
<div class="sidebar">
<NavMenu />
</div>
<div class="main">
<div class="top-row px-4">
<a rel='nofollow' href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
</div>
<div class="content px-4">
@Body
</div>
<SfSpinner @bind-Visible="@SpinnerVisible" CssClass="e-spin-overlay">
</SfSpinner>
</div>
</CascadingValue>
@code{
public bool SpinnerVisible { get; set; } = false;
public async Task ClickHandler()
{
this.SpinnerVisible = true;
StateHasChanged();
await Task.Delay(2000);
this.SpinnerVisible = false;
StateHasChanged();
}
} |
|
[CascadingParameter]
public MainLayout mainLayoutObj { get; set; }
private async Task ClickHandler()
{
await mainLayoutObj.ClickHandler();
} |
How would I attach that to something like this on my page:
<a rel='nofollow' href="CP/Locations" class="btn btn-cpDashLocations"></a>
to show the spinner until the navigation to the page completes. I do not want to delay the page navigation just to see a spinner. The spinner should only show when navigating to the next page is not instantaneous.
And, for buttons click handlers, where would I put this? At the top of the method or at the bottom?
await mainLayoutObj.ClickHandler();
@using Syncfusion.Blazor.Spinner @using Microsoft.AspNetCore.Components.Routing <Router AppAssembly="typeof(Program).Assembly" OnNavigateAsync="@OnNavigateAsync"> <Found Context="routeData"> <RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" /> <FocusOnNavigate RouteData="routeData" Selector="h1" /> </Found> <Navigating> <div id="spinner-container"> <SfSpinner @bind-Visible="@IsNavigating" CssClass="e-spin-overlay" /> </div> </Navigating> </Router> @code { private bool IsNavigating { get; set; } = false; [Inject] private NavigationManager NavigationManager { get; set; } protected override async Task OnInitializedAsync() { NavigationManager.LocationChanged += HandleLocationChanged; } private void HandleLocationChanged(object sender, LocationChangedEventArgs args) { IsNavigating = true; } private async Task OnNavigateAsync(NavigationContext args) { await Task.Delay(500); // Simulate a delay for demonstration purposes. You can remove it in your application. IsNavigating = false; // Once navigation is complete, hide the spinner } public void Dispose() { NavigationManager.LocationChanged -= HandleLocationChanged; } } <style> #spinner-container { position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; background-color: rgba(255, 255, 255, 0.8); z-index: 1000; } </style> |
Spinner component is displayed when the IsNavigating property is true. The spinner is styled using the e-spin-overlay class for better visibility.NavigationManager.LocationChanged event is used to detect when navigation starts, setting IsNavigating to true.OnNavigateAsync method ensures the spinner is visible during navigation and hides it after navigation completes. The delay (Task.Delay(500)) is added for demonstration and can be removed in your production application.<Router> component handles navigation and manages the spinner display seamlessly.Dispose method ensures the event handler is unregistered to prevent memory leaks.