ant-design-blazor/components/core/Internal/HtmlRenderService.cs
2020-05-29 00:33:49 +08:00

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