Blazor:如何在单击按钮后立即显示加载文本?

huangapple go评论69阅读模式
英文:

Blazor: How to show a loading text immediately after clicking a button?

问题

我有一个 Blazor 页面用于创建用户,当按钮被点击时,我想显示一个加载文本,当完成时,我想隐藏文本,以下是我的代码:

<AuthorizeView>
    <Authorized>
        @if (IsLoading) {
            <div>Loading</div>
        } else {
        <div class="add-user">
            <PageHeader Text="Add User" />

            <form Model="NewUser" class="user-form">
                <div class="line">
                    <label for="name">Name</label>
                    <input type="text" id="name" @bind="NewUser.Name" />
                </div>
                <div class="line">
                    <label for="email">Email</label>
                    <input type="email" id="email" @bind="NewUser.Email" />
                </div>
                <div class="line">
                    <div>
                        <label>Gender</label>
                    </div>
                    <div>
                        <label for="male">Male</label>
                        <input type="radio" id="male" name="gender" value="Male" @onchange="UpdateGender" checked="@IsMaleRadioButtonChecked" />
                        <label for="female">Female</label>
                        <input type="radio" id="female" name="gender" value="Female" @onchange="UpdateGender" checked="@IsFemaleRadioButtonChecked"/>
                    </div>
                </div>
                <div class="line">
                    <label for="date-of-birth">Date of Birth</label>
                    <input type="date" id="date-of-birth" @bind="NewUser.DateOfBirth" />
                </div>
                <Button Text="Add User" OnClick="Save" />
            </form>
        </div>
        }
    </Authorized>
</AuthorizeView>

@code {
    private async void Save()
    {
        IsLoading = true;
        StateHasChanged();

        if (!AreFormValuesValid()) return;

        NewUser.Id = 0;
        NewUser.Password = NewUser.Name;
        NewUser.Role = roleService.Get("Participant");
        NewUser.Gender = Gender == "Male" ?
            Enumerations.Gender.Male : Enumerations.Gender.Female;

        if (await userService.AddOrUpdate(NewUser))
        {
            js.InvokeVoidAsync("alert", "User Added Successfully");

            NewUser = new User();
            Gender = "";
            StateHasChanged();
        } else
        {
            js.InvokeVoidAsync("alert", "Couldn't Save User");
        }
    
        IsLoading = false;
        StateHasChanged();
    }
}

加载文本在警报后立即显示,但我希望在点击按钮时立即显示加载文本。如何更改我的代码以实现这一点。

我尝试了许多方法,卡在这里已经几个小时了,有人能帮忙吗?(这是一个 Blazor 服务器应用程序项目)

英文:

I have a blazor page that is used to create a user, when the button is clicked, i want to display a loading text. and when it is done, i want to hide the text, here is my code:

&lt;AuthorizeView&gt;
&lt;Authorized&gt;
@if (@IsLoading) {
&lt;div&gt;Loading&lt;/div&gt;
} else {
&lt;div class=&quot;add-user&quot;&gt;
&lt;PageHeader Text=&quot;Add User&quot; /&gt;
&lt;form Model=&quot;NewUser&quot; class=&quot;user-form&quot;&gt;
&lt;div class=&quot;line&quot;&gt;
&lt;label for=&quot;name&quot;&gt;Name&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;name&quot; @bind=&quot;NewUser.Name&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;line&quot;&gt;
&lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
&lt;input type=&quot;email&quot; id=&quot;email&quot; @bind=&quot;NewUser.Email&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;line&quot;&gt;
&lt;div&gt;
&lt;label&gt;Gender&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;label for=&quot;male&quot;&gt;Male&lt;/label&gt;
&lt;input type=&quot;radio&quot; id=&quot;male&quot; name=&quot;gender&quot; value=&quot;Male&quot; @onchange=&quot;UpdateGender&quot; checked=&quot;@IsMaleRadioButtonChecked&quot; /&gt;
&lt;label for=&quot;female&quot;&gt;Female&lt;/label&gt;
&lt;input type=&quot;radio&quot; id=&quot;female&quot; name=&quot;gender&quot; value=&quot;Female&quot;
@onchange=&quot;UpdateGender&quot; checked=&quot;@IsFemaleRadioButtonChecked&quot;/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;line&quot;&gt;
&lt;label for=&quot;date-of-birth&quot;&gt;Date of Birth&lt;/label&gt;
&lt;input type=&quot;date&quot; id=&quot;date-of-birth&quot; @bind=&quot;NewUser.DateOfBirth&quot; /&gt;
&lt;/div&gt;
&lt;Button Text=&quot;Add User&quot; OnClick=&quot;Save&quot; /&gt;
&lt;/form&gt;
&lt;/div&gt;
}
&lt;/Authorized&gt;
&lt;/AuthorizeView&gt;
@code {
private async void Save()
{
IsLoading = true;
StateHasChanged();
if (!AreFormValuesValid()) return;
NewUser.Id = 0;
NewUser.Password = NewUser.Name;
NewUser.Role = roleService.Get(&quot;Participant&quot;);
NewUser.Gender = Gender == &quot;Male&quot; ?
Enumerations.Gender.Male : Enumerations.Gender.Female;
if (await userService.AddOrUpdate(NewUser))
{
js.InvokeVoidAsync(&quot;alert&quot;, &quot;User Added Successfully&quot;);
NewUser = new User();
Gender = &quot;&quot;;
StateHasChanged();
} else
{
js.InvokeVoidAsync(&quot;alert&quot;, &quot;Couldn&#39;t Save User&quot;);
}
IsLoading = false;
StateHasChanged();
}

the loading text appears righ after the alert, but i want the loading text to appear right when i click the button. how do i change my code to achieve that.

i tried a lot of methods, and have been stuck for hours, can anyonw help? (this is a blazor server app project)

答案1

得分: 1

更新用户界面只能在await期间进行。当消息只在警告后出现时,userService.AddOrUpdate()实际上并不是异步的。添加延迟以更新屏幕:

  //private async void Save()   -- 避免使用`async void`
    private async Task Save()
    {
        IsLoading = true;
      //StateHasChanged();      -- 这一行不需要更改
        await Task.Delay(1);

        if (!AreFormValuesValid()) return;
英文:

Updating the UI can only happen during an await.
When the message only appears after the alert then userService.AddOrUpdate() isn't really async. Add a delay to update the screen:

  //private async void Save()   -- avoid `async void`
private async Task Save()
{
IsLoading = true;
//StateHasChanged();      -- you get this one for free
await Task.Delay(1);
if (!AreFormValuesValid()) return;

huangapple
  • 本文由 发表于 2023年6月1日 15:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定