Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Calling async method from inside NavigationManager.LocationChanged

$
0
0

I am using NavigationManager.LocationChanged to capture query strings. After getting the query string value I'm making an ajax call, which is async. LocationChanged itself is synchronous method, and it looks like there is no async version of LocationChanged. And when calling async method from inside LocationChanged, the value set by the async method is lagging one step behind.

Here is the repro:

@page "/investigate"@implements IDisposable@inject NavigationManager NM<h1>Sync: @SyncValue</h1><h1>Async: @AsyncValue</h1><button @onclick="TriggerLocationChange">Increment</button>@code {    private string SyncValue;    private string AsyncValue;    private int Counter = 1;    protected override void OnInitialized()    {        NM.LocationChanged += OnLocationChanged;    }    public void Dispose()    {        NM.LocationChanged -= OnLocationChanged;    }    private void OnLocationChanged(object sender, LocationChangedEventArgs args)    {        // sync action, just for comparison        SyncValue = (Counter * 1000).ToString();        DoSomeAsync();    }    private async Task DoSomeAsync()    {        // http call to server        await Task.Delay(1);        AsyncValue = (Counter * 1000).ToString();    }    private void TriggerLocationChange()    {        Counter++;        NM.NavigateTo("investigate?counter="+ Counter);    }}

The @AsyncValue is lagging one step behind from the @SyncValue.

How can I prevent the async method from lagging behind when called from inside LocationChanged?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>