KEMBAR78
[HybridWebView] Add WebViewInitializing and WebViewInitialized Events by tj-devel709 · Pull Request #29531 · dotnet/maui · GitHub
Skip to content

Conversation

tj-devel709
Copy link
Member

@tj-devel709 tj-devel709 commented May 15, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

Adds initialization events to HybridWebView following the same pattern as BlazorWebView, enabling platform-specific WebView customization before and after initialization.

New Events

  • WebViewInitializing - Fired before WebView creation, allows configuration of platform-specific initialization parameters
  • WebViewInitialized - Fired after WebView creation, provides access to the native WebView instance and settings

Platform Support

  • Windows: WebView2 environment options, user data folder, profile settings, private mode
  • iOS/MacCatalyst: WKWebViewConfiguration access
  • Android: WebView and WebSettings access

Usage Example

var hybridWebView = new HybridWebView();

hybridWebView.WebViewInitializing += (s, e) =>
{
#if IOS || MACCATALYST
    e.PlatformArgs.Configuration.ApplicationNameForUserAgent = "MyApp";
#endif
};

hybridWebView.WebViewInitialized += (s, e) =>
{
#if ANDROID
    e.PlatformArgs.Settings.UserAgentString = "MyApp/1.0";
#elif WINDOWS  
    e.PlatformArgs.Settings.UserAgent = "MyApp/1.0";
#endif
};

Issues

Copilot

This comment was marked as outdated.

Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

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

A few build errors:

C:\a\_work\1\s\src\Core\src\Handlers\HybridWebView\HybridWebViewHandler.Windows.cs(224,26): error CS0120: An object reference is required for the non-static field, method, or property 'ViewHandler<IHybridWebView, WebView2>.VirtualView' [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-windows10.0.19041.0]
C:\a\_work\1\s\src\Core\src\Handlers\HybridWebView\HybridWebViewHandler.Windows.cs(237,5): error CS0120: An object reference is required for the non-static field, method, or property 'ViewHandler<IHybridWebView, WebView2>.VirtualView' [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-windows10.0.19041.0]
C:\Users\cloudtest\.nuget\packages\microsoft.windowsappsdk\1.7.250401001\buildTransitive\Microsoft.UI.Xaml.Markup.Compiler.interop.targets(845,9): error MSB3073: The command ""C:\Users\cloudtest\.nuget\packages\microsoft.windowsappsdk\1.7.250401001\buildTransitive\..\tools\net6.0\..\net472\XamlCompiler.exe" "C:\a\_work\1\s\artifacts\obj\Core\Release\net10.0-windows10.0.19041.0\\input.json" "C:\a\_work\1\s\artifacts\obj\Core\Release\net10.0-windows10.0.19041.0\\output.json"" exited with code 1. [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-windows10.0.19041.0]
C:\a\_work\1\s\src\Core\src\Handlers\HybridWebView\HybridWebViewHandler.Windows.cs(224,26): error CS0120: An object reference is required for the non-static field, method, or property 'ViewHandler<IHybridWebView, WebView2>.VirtualView' [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-windows10.0.20348.0]
C:\a\_work\1\s\src\Core\src\Handlers\HybridWebView\HybridWebViewHandler.Windows.cs(237,5): error CS0120: An object reference is required for the non-static field, method, or property 'ViewHandler<IHybridWebView, WebView2>.VirtualView' [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-windows10.0.20348.0]
C:\Users\cloudtest\.nuget\packages\microsoft.windowsappsdk\1.7.250401001\buildTransitive\Microsoft.UI.Xaml.Markup.Compiler.interop.targets(845,9): error MSB3073: The command ""C:\Users\cloudtest\.nuget\packages\microsoft.windowsappsdk\1.7.250401001\buildTransitive\..\tools\net6.0\..\net472\XamlCompiler.exe" "C:\a\_work\1\s\artifacts\obj\Core\Release\net10.0-windows10.0.20348.0\\input.json" "C:\a\_work\1\s\artifacts\obj\Core\Release\net10.0-windows10.0.20348.0\\output.json"" exited with code 1. [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-windows10.0.20348.0]
    6 Error(s)

@rmarinho rmarinho moved this from Todo to Ready To Review in MAUI SDK Ongoing May 20, 2025
@PureWeen PureWeen force-pushed the dev/TJ/HybridWebView-Initialized2 branch from 800483f to 3edb489 Compare July 17, 2025 21:14
@mattleibow mattleibow force-pushed the dev/TJ/HybridWebView-Initialized2 branch 2 times, most recently from 856502d to 0f92314 Compare July 24, 2025 18:32
Copilot

This comment was marked as outdated.

@mattleibow mattleibow changed the title [HybridWebView] Add Initializing and Initialized Events [HybridWebView] Add WebViewInitializing and WebViewInitialized Events Jul 24, 2025
@mattleibow mattleibow force-pushed the dev/TJ/HybridWebView-Initialized2 branch 2 times, most recently from 330695d to b9f2cf3 Compare July 24, 2025 22:06
tj-devel709 and others added 20 commits August 5, 2025 01:17
# Conflicts:
#	src/Controls/src/Core/HybridWebView/HybridWebView.cs
#	src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt
#	src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt
#	src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt
#	src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt
#	src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt
#	src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt
#	src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt
#	src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs
#	src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt
#	src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt
#	src/Core/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt
#	src/Core/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt
#	src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt
#	src/Core/src/PublicAPI/net/PublicAPI.Unshipped.txt
#	src/Core/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt
#	src/Core/src/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt
# Conflicts:
#	src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests.cs
* Initial plan

* Fix documentation bug in IInitializationAwareWebView.cs

Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
WebView2 does not allow different envvars and some settings in a
single process simultaneously without also configuring different user
data folders.

Also, better to run the webview tests in series as they can confict
with other things we do.
@mattleibow mattleibow force-pushed the dev/TJ/HybridWebView-Initialized2 branch from 429797d to af77aec Compare August 4, 2025 23:17
@mattleibow
Copy link
Member

/backport to release/10.0.1xx-preview7

@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2025

Started backporting to release/10.0.1xx-preview7: https://github.com/dotnet/maui/actions/runs/16754040073

@rmarinho rmarinho merged commit 4e456ba into net10.0 Aug 6, 2025
126 of 129 checks passed
@rmarinho rmarinho deleted the dev/TJ/HybridWebView-Initialized2 branch August 6, 2025 08:38
@github-project-automation github-project-automation bot moved this from Changes Requested to Done in MAUI SDK Ongoing Aug 6, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Sep 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants