mirror of
https://github.com/PacktPublishing/Web-Development-with-Blazor-Second-Edition.git
synced 2026-04-07 23:33:53 +00:00
Initial commit
This commit is contained in:
parent
2190113c56
commit
3088165398
1765 changed files with 192085 additions and 0 deletions
|
|
@ -0,0 +1,42 @@
|
|||
@inject IJSRuntime JSRuntime
|
||||
@implements IDisposable
|
||||
|
||||
<NavigationLock
|
||||
ConfirmExternalNavigation="@(CurrentEditContext.IsModified())"
|
||||
OnBeforeInternalNavigation="OnBeforeInternalNavigation" />
|
||||
|
||||
@code{
|
||||
[CascadingParameter]
|
||||
public required EditContext CurrentEditContext { get; set; }
|
||||
public string InternalNavigationMessage { get; set; } = "You are about to loose changes, are you sure you want to navigate away?";
|
||||
|
||||
protected override Task OnInitializedAsync()
|
||||
{
|
||||
CurrentEditContext.OnFieldChanged += OnFieldChangedAsync;
|
||||
return base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
private async void OnFieldChangedAsync(object? sender,FieldChangedEventArgs args)
|
||||
{
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private async Task OnBeforeInternalNavigation(LocationChangingContext context)
|
||||
{
|
||||
if (CurrentEditContext.IsModified())
|
||||
{
|
||||
var isConfirmed = await JSRuntime.InvokeAsync<bool>("confirm",
|
||||
InternalNavigationMessage);
|
||||
|
||||
if (!isConfirmed)
|
||||
{
|
||||
context.PreventNavigation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IDisposable.Dispose()
|
||||
{
|
||||
CurrentEditContext.OnFieldChanged -= OnFieldChangedAsync;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue