Files
ersatztv/ErsatzTV/Pages/FragmentNavigationBase.cs
T
Jason DoveandGitHub 1ab98578ab refactor namespaces and imports (#670)
* re-namespace

* optimize usings

* more usings

* more of the same

* more implicit/global usings

* cleanup all usings

* minor fixes
2022-03-03 15:36:07 -06:00

58 lines
1.4 KiB
C#

using System.Diagnostics.CodeAnalysis;
using ErsatzTV.Extensions;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.JSInterop;
namespace ErsatzTV.Pages;
public class FragmentNavigationBase : ComponentBase, IDisposable
{
private readonly CancellationTokenSource _cts = new();
protected CancellationToken CancellationToken => _cts.Token;
[Inject]
private NavigationManager NavManager { get; set; }
[Inject]
private IJSRuntime JsRuntime { get; set; }
public void Dispose()
{
NavManager.LocationChanged -= TryFragmentNavigation;
_cts?.Cancel();
_cts?.Dispose();
}
protected override void OnInitialized() => NavManager.LocationChanged += TryFragmentNavigation;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
try
{
await NavManager.NavigateToFragmentAsync(JsRuntime);
}
catch (Exception)
{
// ignored
}
}
}
[SuppressMessage("ReSharper", "VSTHRD100")]
private async void TryFragmentNavigation(object sender, LocationChangedEventArgs args)
{
try
{
await NavManager.NavigateToFragmentAsync(JsRuntime);
}
catch (Exception)
{
// ignored
}
}
}