When the user press on a specific button i need to show an animation inside the button and disable it ( the method called need several seconds to accompish). This is my html:
<div class="row">
<div class="col-lg-4">
@if (IsDownloading)
{
<button class="btn btn-primary float-right search disabled" disabled><span class='fa-left fas fa-sync-alt spinning'></span>Downloading...</button>
}
else
{
<button id="REC_PDF" class="btn btn-primary" @onclick="@(()=>getPDF())">Download Labels</button>
}
</div>
</div>
and this is my code:
protected bool IsDownloading { get; set; }
public async Task getPDF()
{
IsDownloading = true;
StateHasChanged();
await generate_pdf(labels);
IsDownloading = false;
StateHasChanged();
}
private async Task generate_pdf(List<Tuple<string, string, string, string>> filtered_label)
{
loading = true;
string PDF_PATH = "./PDF/" + CurrentValue;
foreach (Tuple<string,string,string,string> label in filtered_label)
{
if (!Directory.Exists(PDF_PATH))
{
Directory.CreateDirectory(PDF_PATH);
}
.......
The code is working but the rendering of the page called by StateHasChanged(); is executed 2 times when the method getPDF ( and generate_PDF consequentially) is fully executed.
I need page rendering on the frist call to show css loading animation.
( as suggested in other post i have used InvokeAsync(() => StateHasChanged()); but is not working)
Thanks.
@onclick="@(()=>getPDF())"<-- Why? By removing all task information and thus not awaiting anything.@onclick="getPDF"is the right way... but not the cause of your issue.