[英]Blazor Server: Changing dynamic component type and accessing it's reference immediately
If i change the component type dynamically dynCompType = typeof(RubricEditor);
如果我动态更改组件类型dynCompType = typeof(RubricEditor);
and then access it's reference to call a method: await ((RubricEditor?)_dynComp?.Instance.);ReloadEntity();
然后访问它的引用以调用方法: await ((RubricEditor?)_dynComp?.Instance.);ReloadEntity();
i get an exception, because "_dynComp" is still pointing to the previously rendered component.我得到一个异常,因为“_dynComp”仍然指向先前呈现的组件。 how can i change the component, wait for re-rendering so the ref is updated, and then call the method in one step?如何更改组件,等待重新渲染以便更新 ref,然后一步调用该方法?
DynamicComponent? _dynComp { get; set; }
async Task SwitchComponent()
{
// dynCompType is "FolderEditor" here -> _dynComp.Instance is of type "FolderEditor"
// change component type
dynCompType = typeof(RubricEditor);
// how to wait here for _dynComp.Instance to change?
xxxx?
// this method call fails because instance still is of type FolderEditor
await ((RubricEditor?)_dynComp?.Instance!).ReloadEntity();
}
i'm following this dynamic component example from the docs: https://learn.microsoft.com/en-us/as.net/core/blazor/components/dynamiccomponent?view=as.netcore-7.0我正在关注文档中的这个动态组件示例: https://learn.microsoft.com/en-us/as.net/core/blazor/components/dynamiccomponent?view=as.netcore-7.0
i tried adding StateHasChanged(); await Task.Yield();
我尝试添加StateHasChanged(); await Task.Yield();
StateHasChanged(); await Task.Yield();
between changing the type and accessing.Instance, but this didn't work.在更改类型和访问实例之间,但这没有用。
I think the 'Blazor Way' would be to call ReloadEntity in the OnInitialized method of both FolderEditor and RubricEditor.我认为“Blazor 方式”是在 FolderEditor 和 RubricEditor 的 OnInitialized 方法中调用 ReloadEntity。
Something like this...像这样的东西......
DynamicComponent? _dynComp { get; set; }
async Task SwitchComponent()
{
dynCompType = typeof(RubricEditor);
}
class RubricEditor : ComponentBase {
protected void OnInitialized() {
ReloadEntity();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.