diff --git a/CPF.Razor/Controls/Element.cs b/CPF.Razor/Controls/Element.cs index fd6fa35..c5ee696 100644 --- a/CPF.Razor/Controls/Element.cs +++ b/CPF.Razor/Controls/Element.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Components; +using CPF.Input; +using Microsoft.AspNetCore.Components; //using Microsoft.MobileBlazorBindings.Core; using System; using System.Collections.Generic; @@ -8,33 +9,100 @@ namespace CPF.Razor.Controls { public abstract class Element : NativeControlComponentBase 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) { 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 (Width != null) - { - builder.AddAttribute(nameof(Width), Width); + if (events.Contains(attributeName)) + { + handlerIds[attributeName] = attributeEventHandlerId; + Renderer.RegisterEvent(attributeEventHandlerId, id => { if (id == attributeEventHandlerId) { handlerIds.Remove(attributeName); } }); + } } } + + Dictionary handlerIds = new Dictionary(); + HashSet events = new HashSet(); + + 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 MouseDown { get; set; } + } } diff --git a/CPF.Razor/Controls/Panel.cs b/CPF.Razor/Controls/Panel.cs index a49199f..e989a47 100644 --- a/CPF.Razor/Controls/Panel.cs +++ b/CPF.Razor/Controls/Panel.cs @@ -6,45 +6,16 @@ using System.Text; namespace CPF.Razor.Controls { + /// + /// 测试 + /// public partial class Panel : Element { - //static Panel() - //{ - // ElementHandlerRegistry.RegisterElementHandler(); - //} - [Parameter] public string Background { get; set; } #pragma warning disable CA1721 // Property names should not match get methods [Parameter] public RenderFragment ChildContent { get; set; } #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; - - 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); - } - } } } diff --git a/CPF.Razor/Core/NativeComponentAdapter.cs b/CPF.Razor/Core/NativeComponentAdapter.cs index 1658948..2e26614 100644 --- a/CPF.Razor/Core/NativeComponentAdapter.cs +++ b/CPF.Razor/Core/NativeComponentAdapter.cs @@ -258,6 +258,10 @@ namespace CPF.Razor //{ // componentInstance.SetElementReference(elementHandler); //} + if (elementHandler is ICpfElementHandler handler) + { + handler.Renderer = Renderer; + } if (siblingIndex != 0) { diff --git a/CPF.Razor/Core/NativeControlComponentBase.cs b/CPF.Razor/Core/NativeControlComponentBase.cs index 79f0ac9..33e69fb 100644 --- a/CPF.Razor/Core/NativeControlComponentBase.cs +++ b/CPF.Razor/Core/NativeControlComponentBase.cs @@ -9,7 +9,7 @@ namespace CPF.Razor { public abstract class NativeControlComponentBase : ComponentBase, ICpfElementHandler where T : UIElement, new() { - public IElementHandler ElementHandler { get; private set; } + //public IElementHandler ElementHandler { get; private set; } UIElement ICpfElementHandler.Element => Element; @@ -28,6 +28,13 @@ namespace CPF.Razor public object TargetElement => Element; + NativeComponentRenderer _Renderer; + public NativeComponentRenderer Renderer + { + get => _Renderer; + set => _Renderer = value; + } + //public void SetElementReference(IElementHandler elementHandler) //{ // ElementHandler = elementHandler ?? throw new ArgumentNullException(nameof(elementHandler)); diff --git a/CPF.Razor/ElementHandler.cs b/CPF.Razor/ElementHandler.cs index f6975b0..c2428bf 100644 --- a/CPF.Razor/ElementHandler.cs +++ b/CPF.Razor/ElementHandler.cs @@ -22,6 +22,8 @@ namespace CPF.Razor public CPF.UIElement Element { get; } public object TargetElement => Element; + NativeComponentRenderer ICpfElementHandler.Renderer { get; set; } + public virtual void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName) { //switch (attributeName) diff --git a/CPF.Razor/ICpfElementHandler.cs b/CPF.Razor/ICpfElementHandler.cs index 403728f..3bda0a4 100644 --- a/CPF.Razor/ICpfElementHandler.cs +++ b/CPF.Razor/ICpfElementHandler.cs @@ -8,5 +8,6 @@ namespace CPF.Razor public interface ICpfElementHandler : IElementHandler { UIElement Element { get; } + NativeComponentRenderer Renderer { get; set; } } } diff --git a/CpfRazorSample/Component1.razor b/CpfRazorSample/Component1.razor new file mode 100644 index 0000000..5a93e1d --- /dev/null +++ b/CpfRazorSample/Component1.razor @@ -0,0 +1,4 @@ + +@code { + +} diff --git a/CpfRazorSample/Test.razor b/CpfRazorSample/Test.razor index 9483d06..e29b8c7 100644 --- a/CpfRazorSample/Test.razor +++ b/CpfRazorSample/Test.razor @@ -1,7 +1,17 @@  - - + + + @if (visible) + { + + } -@**@ -@**@ \ No newline at end of file +@code +{ + bool visible = false; + void OnMouseDown() + { + visible = !visible; + } +} \ No newline at end of file