Skip to content

Conversation

simonrozsival
Copy link
Member

@simonrozsival simonrozsival commented Jun 12, 2025

This is a follow-up to #28357

I modified all built-in controls to use [ElementHandler<THandler>] instead of DI. This allowed me to remove all of AppHostBuilderExtensions.AddControlsHandlers().

I needed to add a new [ElementHandlerWithAndroidContext<THandler>] + interface IElementHandlerWithAndroidContext<THandler> which allows us to create handlers that need injecting the Android context in the constructor. This works perfectly well for our internal use, but I'm not sure if this will be an acceptable API to expose to our customers. This attribute is currently needed for 3 compatibility handlers: FrameRenderer (deprecated in .NET 9), ListViewRenderer (deprecated in .NET 10), TableViewRenderer (deprecated in .NET 10). I might revisit this before I mark this PR as ready for review.

It was necessary to make changes to MauiHandlerFactory to correctly resolve handlers when they were explicitly registered via DI. This is tricky because our handler resolution algorithm is pretty complicated. I wonder if we should revisit and simplify this part (it's not clear to me how much of a breaking change this could be for our customers).

TODO:

  • meassure the final app difference for the template app and for the sample content app (it won't be too dramatic, the default styles + the custom marking logic for NSObjects/Java objects will keep most of the platform handlers in the app anyway.. for now)
  • meassure startup time difference (there's less setup

@Copilot Copilot AI review requested due to automatic review settings June 12, 2025 07:43
@simonrozsival simonrozsival requested a review from a team as a code owner June 12, 2025 07:43
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR transitions built-in controls from DI registration to using [ElementHandler] attributes, introduces IElementHandlerWithAndroidContext<T> for Android renderers, and adds a new MSBuild property to toggle CSS support.

  • Apply [ElementHandler] to core controls and cells instead of DI
  • Introduce IElementHandlerWithAndroidContext<T> and CreateHandler(Context) on Android renderers
  • Add MauiCssEnabled MSBuild property and corresponding runtime host configuration

Reviewed Changes

Copilot reviewed 120 out of 120 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Controls/src/Core/ContentPage/ContentPage.Mapper.cs Added static ctor calling RemapForControls and VisualElement.RemapIfNeeded
src/Controls/src/Core/Compatibility/Handlers/TableView/Android/TableViewRenderer.cs Implemented IElementHandlerWithAndroidContext<TableViewRenderer> and CreateHandler
src/Controls/src/Core/Compatibility/Handlers/ListView/Android/ListViewRenderer.cs Implemented IElementHandlerWithAndroidContext<ListViewRenderer> and CreateHandler
src/Controls/src/Core/Compatibility/Handlers/Android/FrameRenderer.cs Implemented IElementHandlerWithAndroidContext<FrameRenderer> and CreateHandler
src/Controls/src/Core/Cells/*.cs Added [ElementHandler<...>] attributes for compatibility cell renderers
src/Controls/src/Core/Button/Button.cs Added [ElementHandler<ButtonHandler>]
src/Controls/src/Core//.Mapper.cs Added static ctors calling RemapForControls / RemapIfNeeded
src/Controls/src/Build.Tasks/nuget/.../Microsoft.Maui.Controls.targets Added MauiCssEnabled property and runtime host configuration option
src/Compatibility/Core/src/{iOS,Android}/Platform.cs Updated GetHandler calls to pass MauiContext
Comments suppressed due to low confidence (1)

src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.targets:240

  • Added the MauiCssEnabled build property but no corresponding public documentation under /docs/; please update the XML docs to describe this new feature.
<MauiCssEnabled Condition="'$(MauiCssEnabled)' == '' and '@(MauiCss->Count())' == '0'">false</MauiCssEnabled>

{
#pragma warning disable CS0618 // Type or member is obsolete
public class TableViewRenderer : ViewRenderer<TableView, AListView>
public class TableViewRenderer : ViewRenderer<TableView, AListView>, IElementHandlerWithAndroidContext<TableViewRenderer>
Copy link

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing IElementHandlerWithAndroidContext introduces a new public interface on this renderer, which is a breaking API change; please assess version impact.

Copilot uses AI. Check for mistakes.

{
#pragma warning disable CS0618 // Type or member is obsolete
public class ListViewRenderer : ViewRenderer<ListView, AListView>
public class ListViewRenderer : ViewRenderer<ListView, AListView>, IElementHandlerWithAndroidContext<ListViewRenderer>
Copy link

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing IElementHandlerWithAndroidContext introduces a new public interface on this renderer, which is a breaking API change; please assess version impact.

Copilot uses AI. Check for mistakes.

{
[Obsolete("Frame is obsolete as of .NET 9. Please use Border instead.")]
public class FrameRenderer : CardView, IPlatformViewHandler
public class FrameRenderer : CardView, IPlatformViewHandler, IElementHandlerWithAndroidContext<FrameRenderer>
Copy link

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing IElementHandlerWithAndroidContext introduces a new public interface on this renderer, which is a breaking API change; please assess version impact.

Copilot uses AI. Check for mistakes.

AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsHybridWebViewSupported", out bool isSupported)
? isSupported
: IsHybridWebViewSupportedByDefault;
#pragma warning restore IL4000
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#pragma warning restore IL4000

@simonrozsival simonrozsival marked this pull request as draft June 12, 2025 08:06
@mattleibow
Copy link
Member

/azp run

Copy link

Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@PureWeen PureWeen added this to the .NET 10.0-preview6 milestone Jun 17, 2025
@PureWeen PureWeen moved this from Todo to In Progress in MAUI SDK Ongoing Jun 17, 2025
@simonrozsival simonrozsival force-pushed the dev/simonrozsival/net10.0-use-element-handler-attributes branch from 33afe18 to c49e57e Compare June 18, 2025 11:53
@PureWeen PureWeen added platform/macos macOS / Mac Catalyst platform/windows platform/android platform/ios area-architecture Issues with code structure, SDK structure, implementation details labels Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-architecture Issues with code structure, SDK structure, implementation details platform/android platform/ios platform/macos macOS / Mac Catalyst platform/windows

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants