-
Notifications
You must be signed in to change notification settings - Fork 2k
Add UsePlatformHandler<T> for custom BlazorWebView backends #34225
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3bcd285
Add UsePlatformHandler<T> extension for custom BlazorWebView backends
Redth c6b040e
Apply review feedback: tighten constraint to IViewHandler and fix tes…
Redth b986f5a
Merge remote-tracking branch 'origin/main' into dev/redth/fix-34103
Redth 1da8458
Address review feedback: add new() constraint and factory-based overload
Redth f7e68c1
Add builder null checks and tighten factory overload to IViewHandler
Redth a41994f
Address multi-model code-review feedback
Copilot f146082
Fix unresolved cref to IServiceCollection in XML doc
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/BlazorWebView/src/Maui/MauiBlazorWebViewBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Maui; | ||
| using Microsoft.Maui.Hosting; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.WebView.Maui | ||
| { | ||
| /// <summary> | ||
| /// Extension methods for <see cref="IMauiBlazorWebViewBuilder"/>. | ||
| /// </summary> | ||
| public static class MauiBlazorWebViewBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Registers a custom handler for <see cref="IBlazorWebView"/>, replacing the default | ||
| /// <see cref="BlazorWebViewHandler"/> registered by | ||
| /// <see cref="M:Microsoft.Extensions.DependencyInjection.BlazorWebViewServiceCollectionExtensions.AddMauiBlazorWebView(Microsoft.Extensions.DependencyInjection.IServiceCollection)"/>. | ||
| /// This allows custom platform backends to provide their own BlazorWebView handler | ||
| /// while reusing all shared service registrations. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Replacement is "last-registration-wins" through the underlying MAUI handler collection. | ||
| /// Call this method <em>after</em> <c>AddMauiBlazorWebView()</c> so the custom handler | ||
| /// overrides the default registration. If a downstream library calls | ||
| /// <c>AddMauiBlazorWebView()</c> again later in the pipeline, that subsequent default | ||
| /// registration will silently re-override this custom handler — call this method last, | ||
| /// after every other library's MAUI Blazor configuration, when composing multiple sources. | ||
| /// </remarks> | ||
| /// <typeparam name="THandler">The custom handler type to use for <see cref="IBlazorWebView"/>. | ||
| /// Must have a public parameterless constructor.</typeparam> | ||
| /// <param name="builder">The <see cref="IMauiBlazorWebViewBuilder"/>.</param> | ||
| /// <returns>The <see cref="IMauiBlazorWebViewBuilder"/> for chaining.</returns> | ||
| public static IMauiBlazorWebViewBuilder UsePlatformHandler<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>( | ||
| this IMauiBlazorWebViewBuilder builder) | ||
| where THandler : IViewHandler, new() | ||
| { | ||
| ArgumentNullException.ThrowIfNull(builder); | ||
| builder.Services.ConfigureMauiHandlers(handlers => | ||
| handlers.AddHandler<IBlazorWebView, THandler>()); | ||
| return builder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Registers a custom handler for <see cref="IBlazorWebView"/> using a factory method, | ||
| /// replacing the default <see cref="BlazorWebViewHandler"/> registered by | ||
| /// <see cref="M:Microsoft.Extensions.DependencyInjection.BlazorWebViewServiceCollectionExtensions.AddMauiBlazorWebView(Microsoft.Extensions.DependencyInjection.IServiceCollection)"/>. | ||
| /// Use this overload for handlers that lack a public parameterless constructor or that | ||
| /// need to pull dependencies from the MAUI handler service container at construction time. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The <see cref="IServiceProvider"/> passed to <paramref name="factory"/> is the MAUI | ||
| /// handler factory's service provider, not the application's root <see cref="IServiceProvider"/>. | ||
| /// It can resolve services that were registered on the handler collection (via | ||
| /// <c>ConfigureMauiHandlers</c>); it cannot resolve arbitrary services from the app's | ||
| /// <see cref="Microsoft.Extensions.DependencyInjection.IServiceCollection"/>. The same call-ordering rule as | ||
| /// <see cref="UsePlatformHandler{THandler}(IMauiBlazorWebViewBuilder)"/> applies — call this | ||
| /// method after <c>AddMauiBlazorWebView()</c> (and after any later re-invocations from | ||
| /// downstream libraries) so the custom handler is the last registration to win. | ||
| /// </remarks> | ||
| /// <param name="builder">The <see cref="IMauiBlazorWebViewBuilder"/>.</param> | ||
| /// <param name="factory">A factory function that creates the handler instance. | ||
| /// The <see cref="IServiceProvider"/> argument is the MAUI handler factory's service provider | ||
| /// (see remarks).</param> | ||
| /// <returns>The <see cref="IMauiBlazorWebViewBuilder"/> for chaining.</returns> | ||
| public static IMauiBlazorWebViewBuilder UsePlatformHandler( | ||
| this IMauiBlazorWebViewBuilder builder, | ||
| Func<IServiceProvider, IViewHandler> factory) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(builder); | ||
| ArgumentNullException.ThrowIfNull(factory); | ||
| builder.Services.ConfigureMauiHandlers(handlers => | ||
| handlers.AddHandler<IBlazorWebView>(factory)); | ||
| return builder; | ||
| } | ||
| } | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
src/BlazorWebView/src/Maui/PublicAPI/net-android/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| #nullable enable | ||
| Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder, System.Func<System.IServiceProvider!, Microsoft.Maui.IViewHandler!>! factory) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler<THandler>(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! | ||
| override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.ConnectHandler(Android.Webkit.WebView! platformView) -> void |
3 changes: 3 additions & 0 deletions
3
src/BlazorWebView/src/Maui/PublicAPI/net-ios/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| #nullable enable | ||
| Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder, System.Func<System.IServiceProvider!, Microsoft.Maui.IViewHandler!>! factory) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler<THandler>(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! |
3 changes: 3 additions & 0 deletions
3
src/BlazorWebView/src/Maui/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| #nullable enable | ||
| Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder, System.Func<System.IServiceProvider!, Microsoft.Maui.IViewHandler!>! factory) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler<THandler>(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! |
3 changes: 3 additions & 0 deletions
3
src/BlazorWebView/src/Maui/PublicAPI/net-tizen/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| #nullable enable | ||
| Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder, System.Func<System.IServiceProvider!, Microsoft.Maui.IViewHandler!>! factory) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler<THandler>(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! |
3 changes: 3 additions & 0 deletions
3
src/BlazorWebView/src/Maui/PublicAPI/net-windows/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| #nullable enable | ||
| Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder, System.Func<System.IServiceProvider!, Microsoft.Maui.IViewHandler!>! factory) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler<THandler>(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! |
3 changes: 3 additions & 0 deletions
3
src/BlazorWebView/src/Maui/PublicAPI/net/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| #nullable enable | ||
| Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder, System.Func<System.IServiceProvider!, Microsoft.Maui.IViewHandler!>! factory) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! | ||
| static Microsoft.AspNetCore.Components.WebView.Maui.MauiBlazorWebViewBuilderExtensions.UsePlatformHandler<THandler>(this Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! builder) -> Microsoft.AspNetCore.Components.WebView.Maui.IMauiBlazorWebViewBuilder! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
UsePlatformHandler<THandler>()registers the handler viahandlers.AddHandler<IBlazorWebView, THandler>(), which ultimately creates handler instances usingActivator.CreateInstance(ImplementationType)inMauiFactory. This requiresTHandlerto have a public parameterless constructor; otherwise it will fail at runtime with aMissingMethodException. Consider enforcing this contract with anew()generic constraint, or switch to the factory overload (AddHandler<TType>(Func<IServiceProvider, IElementHandler>)) so handlers with non-parameterless constructors can be created safely.