From bbb317f16f6857bcd86d627862eb9dca2e991d4e Mon Sep 17 00:00:00 2001 From: Oleksandr Liakhevych Date: Wed, 6 Nov 2024 02:03:04 +0200 Subject: [PATCH] Enable IsAotCompatible. Fix Trim warnings --- .../BlazorBindings.Core.csproj | 2 +- .../BlazorBindings.Maui.csproj | 2 +- .../DataTemplateSelectorComponent.cs | 83 ++++++++++--------- .../SyncDataTemplateItemsComponent.cs | 42 +++++----- 4 files changed, 65 insertions(+), 64 deletions(-) diff --git a/src/BlazorBindings.Core/BlazorBindings.Core.csproj b/src/BlazorBindings.Core/BlazorBindings.Core.csproj index c695fd4c..da1a53b1 100644 --- a/src/BlazorBindings.Core/BlazorBindings.Core.csproj +++ b/src/BlazorBindings.Core/BlazorBindings.Core.csproj @@ -6,7 +6,7 @@ Common logic for using Blazor to target native renderers. For mobile app projects for Android and iOS use the BlazorBindings.Maui package. blazor;blazorbindings enable - true + true diff --git a/src/BlazorBindings.Maui/BlazorBindings.Maui.csproj b/src/BlazorBindings.Maui/BlazorBindings.Maui.csproj index 78d6559b..55647010 100644 --- a/src/BlazorBindings.Maui/BlazorBindings.Maui.csproj +++ b/src/BlazorBindings.Maui/BlazorBindings.Maui.csproj @@ -5,7 +5,7 @@ Maui binding for Blazor Maui Blazor Bindings enables using Blazor syntax in .razor files to build native apps for iOS and Android. blazor;blazorbindings - true + true 10.0 13.1 diff --git a/src/BlazorBindings.Maui/Elements/Internal/DataTemplates/DataTemplateSelectorComponent.cs b/src/BlazorBindings.Maui/Elements/Internal/DataTemplates/DataTemplateSelectorComponent.cs index 70e4efcb..40f337f8 100644 --- a/src/BlazorBindings.Maui/Elements/Internal/DataTemplates/DataTemplateSelectorComponent.cs +++ b/src/BlazorBindings.Maui/Elements/Internal/DataTemplates/DataTemplateSelectorComponent.cs @@ -57,7 +57,7 @@ private int AddTemplateGroup() void INonPhysicalChild.SetParent(object parentElement) { var parent = (TControl)parentElement; - var dataTemplate = new DataTemplateSelector(TemplateSelector, AddTemplateGroup, AddTemplateRootToGroup); + var dataTemplate = new DataTemplateSelector(TemplateSelector, AddTemplateGroup, AddTemplateRootToGroup); SetDataTemplateSelectorAction(parent, dataTemplate); } @@ -66,57 +66,58 @@ void INonPhysicalChild.RemoveFromParent(object parentElement) { } void IContainerElementHandler.AddChild(object child, int physicalSiblingIndex) { } void IContainerElementHandler.RemoveChild(object child, int physicalSiblingIndex) { } - class DataTemplateSelector : MC.DataTemplateSelector +} + +file class DataTemplateSelector : MC.DataTemplateSelector +{ + private readonly RenderFragment _renderFragment; + private readonly Func _addGroupFunc; + private readonly Func _addRootToGroupFunc; + private readonly Dictionary _dataTemplates = new(); + + public DataTemplateSelector(RenderFragment renderFragment, Func addGroupFunc, Func addRootToGroupFunc) { - private readonly RenderFragment _renderFragment; - private readonly Func _addGroupFunc; - private readonly Func _addRootToGroupFunc; - private readonly Dictionary _dataTemplates = new(); + _renderFragment = renderFragment; + _addGroupFunc = addGroupFunc; + _addRootToGroupFunc = addRootToGroupFunc; + } - public DataTemplateSelector(RenderFragment renderFragment, Func addGroupFunc, Func addRootToGroupFunc) + protected override MC.DataTemplate OnSelectTemplate(object item, MC.BindableObject container) + { + var componentType = GetComponentType(_renderFragment((TItem)item)); + + if (!_dataTemplates.TryGetValue(componentType, out var dataTemplate)) { - _renderFragment = renderFragment; - _addGroupFunc = addGroupFunc; - _addRootToGroupFunc = addRootToGroupFunc; + var groupIndex = _addGroupFunc(); + dataTemplate = new MC.DataTemplate(() => _addRootToGroupFunc(groupIndex)); + _dataTemplates[componentType] = dataTemplate; } - protected override MC.DataTemplate OnSelectTemplate(object item, MC.BindableObject container) + return dataTemplate; + } + + static readonly RenderTreeBuilder InspectRenderTreeBuilder = new(); + private static Type GetComponentType(RenderFragment renderFragment) + { + // Consider reworking if https://github.com/dotnet/aspnetcore/issues/17200 is implemented. + try { - var componentType = GetComponentType(_renderFragment((TItem)item)); + renderFragment(InspectRenderTreeBuilder); + var frames = InspectRenderTreeBuilder.GetFrames(); - if (!_dataTemplates.TryGetValue(componentType, out var dataTemplate)) + for (var i = 0; i < frames.Count; i++) { - var groupIndex = _addGroupFunc(); - dataTemplate = new MC.DataTemplate(() => _addRootToGroupFunc(groupIndex)); - _dataTemplates[componentType] = dataTemplate; + if (frames.Array[i].FrameType == RenderTreeFrameType.Component) + { + return frames.Array[i].ComponentType; + } } - return dataTemplate; + throw new InvalidOperationException("RenderFragment does not contain any components."); } - - static readonly RenderTreeBuilder InspectRenderTreeBuilder = new(); - private static Type GetComponentType(RenderFragment renderFragment) + finally { - // Consider reworking if https://github.com/dotnet/aspnetcore/issues/17200 is implemented. - try - { - renderFragment(InspectRenderTreeBuilder); - var frames = InspectRenderTreeBuilder.GetFrames(); - - for (var i = 0; i < frames.Count; i++) - { - if (frames.Array[i].FrameType == RenderTreeFrameType.Component) - { - return frames.Array[i].ComponentType; - } - } - - throw new InvalidOperationException("RenderFragment does not contain any components."); - } - finally - { - InspectRenderTreeBuilder.Clear(); - } + InspectRenderTreeBuilder.Clear(); } } -} +} \ No newline at end of file diff --git a/src/BlazorBindings.Maui/Elements/Internal/DataTemplates/SyncDataTemplateItemsComponent.cs b/src/BlazorBindings.Maui/Elements/Internal/DataTemplates/SyncDataTemplateItemsComponent.cs index 10b17419..8a0b0a6f 100644 --- a/src/BlazorBindings.Maui/Elements/Internal/DataTemplates/SyncDataTemplateItemsComponent.cs +++ b/src/BlazorBindings.Maui/Elements/Internal/DataTemplates/SyncDataTemplateItemsComponent.cs @@ -46,7 +46,7 @@ public MC.BindableObject AddTemplateRoot(TItem initialItem) void INonPhysicalChild.SetParent(object parentElement) { var parent = (TControl)parentElement; - var dataTemplate = new DataTemplateSelector(AddTemplateRoot); + var dataTemplate = new DataTemplateSelector(AddTemplateRoot); SetDataTemplateAction(parent, dataTemplate); } @@ -54,27 +54,27 @@ void INonPhysicalChild.RemoveFromParent(object parentElement) { } object IElementHandler.TargetElement => null; void IContainerElementHandler.AddChild(object child, int physicalSiblingIndex) { } void IContainerElementHandler.RemoveChild(object child, int physicalSiblingIndex) { } +} - // In order to be able to render the item synchronously, we need to have an item upfront, before the render. - // Unfortunately, regular DataTemplate does not have an access to the item, it is set set BindingContext afterwards. - // In order to workaround this issue, we use a DataTemplateSelector, which returns the same DataTemplate. But we're able - // to store the item from OnSelectTemplate method, which is used to render the item in DataTemplate. - class DataTemplateSelector : MC.DataTemplateSelector - { - private readonly MC.DataTemplate _dataTemplate; - private readonly Func _loadTemplate; - private TItem _initialItem; +// In order to be able to render the item synchronously, we need to have an item upfront, before the render. +// Unfortunately, regular DataTemplate does not have an access to the item, it is set set BindingContext afterwards. +// In order to workaround this issue, we use a DataTemplateSelector, which returns the same DataTemplate. But we're able +// to store the item from OnSelectTemplate method, which is used to render the item in DataTemplate. +file class DataTemplateSelector : MC.DataTemplateSelector +{ + private readonly MC.DataTemplate _dataTemplate; + private readonly Func _loadTemplate; + private TItem _initialItem; - public DataTemplateSelector(Func loadTemplate) - { - _loadTemplate = loadTemplate; - _dataTemplate = new MC.DataTemplate(() => _loadTemplate(_initialItem)); - } + public DataTemplateSelector(Func loadTemplate) + { + _loadTemplate = loadTemplate; + _dataTemplate = new MC.DataTemplate(() => _loadTemplate(_initialItem)); + } - protected override MC.DataTemplate OnSelectTemplate(object item, MC.BindableObject container) - { - _initialItem = (TItem)item; - return _dataTemplate; - } + protected override MC.DataTemplate OnSelectTemplate(object item, MC.BindableObject container) + { + _initialItem = (TItem)item; + return _dataTemplate; } -} +} \ No newline at end of file