using System.Threading.Tasks; using Microsoft.AspNetCore.Components; namespace AntDesign { public class HtmlRenderService { private readonly HtmlRenderer _htmlRenderer; public HtmlRenderService(HtmlRenderer htmlRenderer) { _htmlRenderer = htmlRenderer; } public async ValueTask RenderAsync(RenderFragment renderFragment) { var text = await _htmlRenderer.Dispatcher.InvokeAsync(() => _htmlRenderer.RenderComponentAsync(new EmptyComponent(renderFragment), ParameterView.Empty)); return string.Join("", text.Tokens); } private class EmptyComponent : IComponent { private RenderHandle _renderHandle; private readonly RenderFragment _content; public EmptyComponent(RenderFragment content) { this._content = content; } public void Attach(RenderHandle renderHandle) { _renderHandle = renderHandle; } public Task SetParametersAsync(ParameterView parameters) { _renderHandle.Render(_content); return Task.CompletedTask; } } } }