CPF/CPF.Razor/Core/NativeControlComponentBase.cs

77 lines
2.1 KiB
C#
Raw Normal View History

2024-01-08 10:55:10 +08:00
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using System;
namespace CPF.Razor
{
public abstract class NativeControlComponentBase<T> : ComponentBase, ICpfElementHandler where T : UIElement, new()
{
2024-01-08 17:37:26 +08:00
//public IElementHandler ElementHandler { get; private set; }
2024-01-08 10:55:10 +08:00
UIElement ICpfElementHandler.Element => Element;
T element;
public T Element
{
get
{
if (element == null)
{
element = CreateElement();
}
return element;
}
}
public object TargetElement => Element;
2024-01-08 17:37:26 +08:00
NativeComponentRenderer _Renderer;
public NativeComponentRenderer Renderer
{
get => _Renderer;
set => _Renderer = value;
}
2024-01-09 16:57:27 +08:00
public int ComponentId { get ; set; }
2024-01-08 17:37:26 +08:00
2024-01-08 10:55:10 +08:00
//public void SetElementReference(IElementHandler elementHandler)
//{
// ElementHandler = elementHandler ?? throw new ArgumentNullException(nameof(elementHandler));
//}
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.OpenElement(0, GetType().FullName);
RenderAttributes(new AttributesBuilder(builder));
2024-01-09 00:39:58 +08:00
var childContent = GetChild();
2024-01-08 10:55:10 +08:00
if (childContent != null)
{
builder.AddContent(2, childContent);
}
builder.CloseElement();
}
protected virtual void RenderAttributes(AttributesBuilder builder)
{
}
2024-01-09 00:39:58 +08:00
protected virtual RenderFragment GetChild() => null;
2024-01-08 10:55:10 +08:00
public abstract void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName);
protected virtual T CreateElement()
{
return new T();
}
}
}