mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-09 23:47:36 +08:00
44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
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<string> 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;
|
|
}
|
|
}
|
|
}
|
|
} |