Blazor - 如何动态创建组件 [英] Blazor - How to create Components dynamically

查看:58
本文介绍了Blazor - 如何动态创建组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试是否可以动态创建 Blazor 组件.

I want to test if it was possible to create Blazor components dynamically.

我找不到任何方法来做到这一点.我对 此链接 上的一些动态内容进行了一些试验,但还没有结束有任何结果.

I can't find any way to do this. I have experimented a bit with some dynamic content found on this link but have not ended up with any results.

推荐答案

对于 0.2 版,这是 Steve Sanderson 的回答:

For version 0.2 this is the answer from Steve Sanderson:

我们将在未来实现更好的 API 来构建 RenderFragments,但是现在你可以

We'll implement nicer APIs to build RenderFragments in the future, but for now you can

@CreateDynamicComponent();
@functions {
    RenderFragment CreateDynamicComponent() => builder =>
    {
        builder.OpenComponent(0, typeof(SurveyPrompt));
        builder.AddAttribute(1, "Title", "Some title");
        builder.CloseComponent();
    };
}

那些是非常低级的 API(甚至没有记录)所以我们希望不要许多人现在需要这样做.用于此的更高级别的 API 将晚点来.

Those are very low-level APIs (not even documented) so we hope not many people need to do this right now. Higher-level APIs for this will come later.

找到这里

这篇关于Blazor - 如何动态创建组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆