Blazor 组件生命周期 - 在设置参数之后但在绑定之前如何在组件中执行异步操作

Blazor Component Lifecycle - How do you do something async in the component after parameters are set but before they bind

我有一个弹出的对话框组件来编辑网格的一行。

当您单击该行时,我设置了一个 selectedItem 变量,它绑定到我的对话框组件的一个参数并使对话框可见。

<MyDialog @bind-MyObject="selectedItem" @bind-Visible="dialogVisible" />

问题是 MyDialog 组件需要从 selectedItem 获取 属性,然后对 API 进行异步调用并在 selectedItem 绑定之前加载一些数据到对话框中的所有字段。

如果我在 OnParametersSetAsync() 中调用 API,为时已晚,selectedItem 已经绑定。

如果我尝试使用 SetParametersAsync(ParameterView parameters),如果您在调用绑定参数的基本实现之前尝试执行一些异步操作,则会出现错误。

这似乎是一个相当正常的场景,这里有另一种方法吗?

有条件地显示绑定元素:

@if (_ready)
{
    <label>@MyObject.SomeProperty</label>
}

@code {
    [Parameter] public MyClass MyObject { get; set; }

    // Do not create elements bound to parameter until I say
    bool _ready = false;   

    protected override async Task OnParametersSetAsync()
    {
        await MyApi.GetSomeMoreData();
        // Done what I need to do, you can render the bound elements now
        _ready = true;   
    }
}