-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[net10.0] Use ElementHandlerAttribute instead of DI for built-in visual elements #29952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: net10.0
Are you sure you want to change the base?
[net10.0] Use ElementHandlerAttribute instead of DI for built-in visual elements #29952
Conversation
There was a problem hiding this 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>
andCreateHandler(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> |
Copilot
AI
Jun 12, 2025
There was a problem hiding this comment.
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> |
Copilot
AI
Jun 12, 2025
There was a problem hiding this comment.
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> |
Copilot
AI
Jun 12, 2025
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#pragma warning restore IL4000 |
/azp run |
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. |
…ameRenderer and TableViewRenderer
33afe18
to
c49e57e
Compare
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 ofAppHostBuilderExtensions.AddControlsHandlers()
.I needed to add a new
[ElementHandlerWithAndroidContext<THandler>]
+ interfaceIElementHandlerWithAndroidContext<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: