-
Notifications
You must be signed in to change notification settings - Fork 2k
[Extensibility] BlazorWebView: Add custom platform handler registration #36658
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
kubaflo
merged 10 commits into
dotnet:net11.0
from
kubaflo:kubaflo/34103-blazor-handler-net11
Jul 30, 2026
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f02ed15
Add UsePlatformHandler<T> for custom BlazorWebView backends (#34225)
Redth e8fc214
Require BlazorWebView handler capabilities
Copilot aade7ab
Clarify BlazorWebView handler errors
Copilot 3d98b71
Capture BlazorWebView handler once
Copilot 896e360
Align custom Blazor handler nullability
Copilot 0fa8317
Close custom WebView2 handlers on window teardown
Copilot 0460151
Run custom handler factory tests on UI thread
Copilot d25b821
Document Windows custom handler teardown
Copilot a44d103
Merge branch 'net11.0' into kubaflo/34103-blazor-handler-net11
kubaflo 042c163
Merge net11.0 into PR #36658
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() | ||
|
kubaflo marked this conversation as resolved.
Outdated
|
||
| { | ||
| 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) | ||
| { | ||
|
kubaflo marked this conversation as resolved.
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.