Blazor - How to create Components dynamically

前端 未结 2 1812
清酒与你
清酒与你 2020-12-06 04:44

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 foun

2条回答
  •  隐瞒了意图╮
    2020-12-06 05:29

    For version 0.2 this is the answer from Steve Sanderson:

    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();
        };
    }
    

    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.

    Found here

提交回复
热议问题