Razor支持

This commit is contained in:
小红帽 2024-01-08 17:37:26 +08:00
parent 9423dc2d1d
commit f86e000984
8 changed files with 122 additions and 55 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Components; using CPF.Input;
using Microsoft.AspNetCore.Components;
//using Microsoft.MobileBlazorBindings.Core; //using Microsoft.MobileBlazorBindings.Core;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -8,33 +9,100 @@ namespace CPF.Razor.Controls
{ {
public abstract class Element<T> : NativeControlComponentBase<T> where T : UIElement, new() public abstract class Element<T> : NativeControlComponentBase<T> where T : UIElement, new()
{ {
[Parameter] public string MarginLeft { get; set; }
[Parameter] public string MarginTop { get; set; }
[Parameter] public string Width { get; set; }
[Parameter] public string Height { get; set; }
//public CPF.UIElement NativeControl => ((ICpfElementHandler)ElementHandler).Element;
protected override void RenderAttributes(AttributesBuilder builder) protected override void RenderAttributes(AttributesBuilder builder)
{ {
base.RenderAttributes(builder); base.RenderAttributes(builder);
if (MarginLeft != null) var type = GetType();
var ps = type.GetProperties();
foreach (var item in ps)
{ {
builder.AddAttribute(nameof(MarginLeft), MarginLeft); var attr = item.GetCustomAttributes(typeof(ParameterAttribute), true);
if (attr != null && attr.Length > 0 && item.PropertyType != typeof(RenderFragment))
{
var v = item.GetValue(this);
if (v != null)
{
if (item.PropertyType == typeof(EventCallback) || (item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition() == typeof(EventCallback<>)))
{//事件注册还必须加小写的on
builder.AddAttribute("on" + item.Name, v);
}
else
{
builder.AddAttribute(item.Name, v);
}
}
}
} }
if (MarginTop != null)
//if (MarginLeft != null)
//{
// builder.AddAttribute(nameof(MarginLeft), MarginLeft);
//}
//if (MarginTop != null)
//{
// builder.AddAttribute(nameof(MarginTop), MarginTop);
//}
//if (Height != null)
//{
// builder.AddAttribute(nameof(Height), Height);
//}
//if (Width != null)
//{
// builder.AddAttribute(nameof(Width), Width);
//}
}
public override void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName)
{
var p = Element.GetPropertyMetadata(attributeName);
if (p != null)
{ {
builder.AddAttribute(nameof(MarginTop), MarginTop); Element.SetValue(attributeValue.ConvertTo(p.PropertyType), attributeName);
} }
if (Height != null) else
{ {
builder.AddAttribute(nameof(Height), Height); if (events.Contains(attributeName))
} {
if (Width != null) handlerIds[attributeName] = attributeEventHandlerId;
{ Renderer.RegisterEvent(attributeEventHandlerId, id => { if (id == attributeEventHandlerId) { handlerIds.Remove(attributeName); } });
builder.AddAttribute(nameof(Width), Width); }
} }
} }
Dictionary<string, ulong> handlerIds = new Dictionary<string, ulong>();
HashSet<string> events = new HashSet<string>();
protected override T CreateElement()
{
var r = base.CreateElement();
var type = typeof(T);
var ps = type.GetEvents();
foreach (var item in ps)
{
var name = "on" + item.Name;
events.Add(name);
r.Commands.Add(item.Name, (s, e) =>
{
if (handlerIds.TryGetValue(name, out var id))
{
Renderer.Dispatcher.InvokeAsync(() => Renderer.DispatchEventAsync(id, null, e as EventArgs));
}
});
}
return r;
}
//只要属性和事件自动生成就行
[Parameter] public string Name { get; set; }
[Parameter] public FloatField? MarginLeft { get; set; }
[Parameter] public FloatField? MarginTop { get; set; }
[Parameter] public FloatField? MarginBottom { get; set; }
[Parameter] public FloatField? MarginRight { get; set; }
[Parameter] public CPF.FloatField? Width { get; set; }
[Parameter] public CPF.FloatField? Height { get; set; }
[Parameter] public EventCallback<MouseButtonEventArgs> MouseDown { get; set; }
} }
} }

View File

@ -6,45 +6,16 @@ using System.Text;
namespace CPF.Razor.Controls namespace CPF.Razor.Controls
{ {
/// <summary>
/// 测试
/// </summary>
public partial class Panel : Element<CPF.Controls.Panel> public partial class Panel : Element<CPF.Controls.Panel>
{ {
//static Panel()
//{
// ElementHandlerRegistry.RegisterElementHandler<Panel>();
//}
[Parameter] public string Background { get; set; } [Parameter] public string Background { get; set; }
#pragma warning disable CA1721 // Property names should not match get methods #pragma warning disable CA1721 // Property names should not match get methods
[Parameter] public RenderFragment ChildContent { get; set; } [Parameter] public RenderFragment ChildContent { get; set; }
#pragma warning restore CA1721 // Property names should not match get methods #pragma warning restore CA1721 // Property names should not match get methods
protected override void RenderAttributes(AttributesBuilder builder)
{
base.RenderAttributes(builder);
if (Background != null)
{
builder.AddAttribute(nameof(Background), Background);
}
}
protected override RenderFragment GetChildContent() => ChildContent; protected override RenderFragment GetChildContent() => ChildContent;
public override void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName)
{
//switch (attributeName)
//{
// //case nameof(AutoScroll):
// // AutoScroll = AttributeHelper.GetBool(attributeValue);
// // break;
// default:
// break;
//}
var p = Element.GetPropertyMetadata(attributeName);
if (p != null)
{
Element.SetValue(attributeValue.ConvertTo(p.PropertyType), attributeName);
}
}
} }
} }

View File

@ -258,6 +258,10 @@ namespace CPF.Razor
//{ //{
// componentInstance.SetElementReference(elementHandler); // componentInstance.SetElementReference(elementHandler);
//} //}
if (elementHandler is ICpfElementHandler handler)
{
handler.Renderer = Renderer;
}
if (siblingIndex != 0) if (siblingIndex != 0)
{ {

View File

@ -9,7 +9,7 @@ namespace CPF.Razor
{ {
public abstract class NativeControlComponentBase<T> : ComponentBase, ICpfElementHandler where T : UIElement, new() public abstract class NativeControlComponentBase<T> : ComponentBase, ICpfElementHandler where T : UIElement, new()
{ {
public IElementHandler ElementHandler { get; private set; } //public IElementHandler ElementHandler { get; private set; }
UIElement ICpfElementHandler.Element => Element; UIElement ICpfElementHandler.Element => Element;
@ -28,6 +28,13 @@ namespace CPF.Razor
public object TargetElement => Element; public object TargetElement => Element;
NativeComponentRenderer _Renderer;
public NativeComponentRenderer Renderer
{
get => _Renderer;
set => _Renderer = value;
}
//public void SetElementReference(IElementHandler elementHandler) //public void SetElementReference(IElementHandler elementHandler)
//{ //{
// ElementHandler = elementHandler ?? throw new ArgumentNullException(nameof(elementHandler)); // ElementHandler = elementHandler ?? throw new ArgumentNullException(nameof(elementHandler));

View File

@ -22,6 +22,8 @@ namespace CPF.Razor
public CPF.UIElement Element { get; } public CPF.UIElement Element { get; }
public object TargetElement => Element; public object TargetElement => Element;
NativeComponentRenderer ICpfElementHandler.Renderer { get; set; }
public virtual void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName) public virtual void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName)
{ {
//switch (attributeName) //switch (attributeName)

View File

@ -8,5 +8,6 @@ namespace CPF.Razor
public interface ICpfElementHandler : IElementHandler public interface ICpfElementHandler : IElementHandler
{ {
UIElement Element { get; } UIElement Element { get; }
NativeComponentRenderer Renderer { get; set; }
} }
} }

View File

@ -0,0 +1,4 @@
<Panel Width="500" Height="500" Background="#000"></Panel>
@code {
}

View File

@ -1,7 +1,17 @@
 
<Panel Background="#f00" Width="100" Height="100"> <Panel Background="#f00" Width="100" Height="100">
<Panel Background="#00f" Width="20" Height="30"></Panel> <Panel Background="#00f" Width="20" Height="30" MouseDown="OnMouseDown">
<Panel Background="#0ff" Width="20" Height="30" MarginLeft="0"></Panel> </Panel>
@if (visible)
{
<Panel Background="#0f0" Width="20" Height="30" MarginLeft="10"></Panel>
}
</Panel> </Panel>
@*<Button></Button>*@ @code
@*<TestElement Test="123"></TestElement>*@ {
bool visible = false;
void OnMouseDown()
{
visible = !visible;
}
}