From 4c8c3478eab9f2c76616e8faf57917e0bc445c4f Mon Sep 17 00:00:00 2001 From: SubhikshaSf4851 Date: Thu, 16 Apr 2026 12:48:16 +0530 Subject: [PATCH 1/4] [Windows] Fix WebView2 regression --- .../HybridWebViewHandler.Windows.cs | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs b/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs index f9cef27e49cd..8284632374c1 100644 --- a/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs +++ b/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs @@ -307,17 +307,38 @@ private async Task TryInitializeWebView2(WebView2 webView) var initializingArgs = new WebViewInitializationStartedEventArgs(); VirtualView?.WebViewInitializationStarted(initializingArgs); - var env = await CoreWebView2Environment.CreateWithOptionsAsync( - browserExecutableFolder: initializingArgs.BrowserExecutableFolder, - userDataFolder: initializingArgs.UserDataFolder, - options: initializingArgs.EnvironmentOptions); + // Only create a custom CoreWebView2Environment when the user has provided custom + // settings. Creating a separate environment with all-default settings conflicts with + // other WebView2 controls (e.g., regular WebView) in the same app that use the + // default environment, because WebView2 requires all controls sharing the same user + // data folder to use compatible environments. When no customizations are needed, + // use EnsureCoreWebView2Async() so this control joins the default shared environment. + bool hasCustomSettings = + initializingArgs.BrowserExecutableFolder != null || + initializingArgs.UserDataFolder != null || + initializingArgs.EnvironmentOptions != null || + initializingArgs.ScriptLocale != null || + initializingArgs.IsInPrivateModeEnabled || + initializingArgs.ProfileName != null; + + if (hasCustomSettings) + { + var env = await CoreWebView2Environment.CreateWithOptionsAsync( + browserExecutableFolder: initializingArgs.BrowserExecutableFolder, + userDataFolder: initializingArgs.UserDataFolder, + options: initializingArgs.EnvironmentOptions); - var options = env.CreateCoreWebView2ControllerOptions(); - options.ScriptLocale = initializingArgs.ScriptLocale; - options.IsInPrivateModeEnabled = initializingArgs.IsInPrivateModeEnabled; - options.ProfileName = initializingArgs.ProfileName; + var options = env.CreateCoreWebView2ControllerOptions(); + options.ScriptLocale = initializingArgs.ScriptLocale; + options.IsInPrivateModeEnabled = initializingArgs.IsInPrivateModeEnabled; + options.ProfileName = initializingArgs.ProfileName; - await webView.EnsureCoreWebView2Async(env, options); + await webView.EnsureCoreWebView2Async(env, options); + } + else + { + await webView.EnsureCoreWebView2Async(); + } webView.CoreWebView2.Settings.AreDevToolsEnabled = Handler?.DeveloperTools.Enabled ?? false; webView.CoreWebView2.Settings.IsWebMessageEnabled = true; From 957a87aa0f6921e76e7921da08970bb61726f80a Mon Sep 17 00:00:00 2001 From: SubhikshaSf4851 Date: Tue, 21 Apr 2026 17:55:58 +0530 Subject: [PATCH 2/4] Test Sample Added --- .../TestCases.HostApp/Issues/Issue34558.cs | 44 +++++++++++++++++++ .../Tests/Issues/Issue34558.cs | 19 ++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs create mode 100644 src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs new file mode 100644 index 000000000000..f3f6d0540e96 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs @@ -0,0 +1,44 @@ +namespace TestCases.HostApp.Issues; + +[Issue(IssueTracker.Github, 34558, "[Windows] WebView renders blank when HybridWebView and WebView coexist in same app", PlatformAffected.UWP)] +public partial class Issue34558 : ContentPage +{ + public Issue34558() + { + var hybridStatusLabel = new Label + { + AutomationId = "HybridStatusLabel", + Text = "Test passes if WebView shows content below without any exception", + }; + + // HybridWebView — initializing this creates a separate CoreWebView2Environment (without fix) + // that conflicts with the regular WebView's default environment. + var hybridWebView = new HybridWebView + { + AutomationId = "HybridWebViewControl", + HybridRoot = "hybridroot", + HeightRequest = 120, + HorizontalOptions = LayoutOptions.Fill, + }; + + var webView = new WebView + { + AutomationId = "RegularWebView", + HeightRequest = 150, + HorizontalOptions = LayoutOptions.Fill, + Source = new HtmlWebViewSource { Html = "

WebView should load here

If you see this, the WebView is working.

" }, + }; + + Content = new VerticalStackLayout + { + Spacing = 10, + Padding = new Thickness(16), + Children = + { + hybridStatusLabel, + hybridWebView, + webView, + } + }; + } +} diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs new file mode 100644 index 000000000000..85e4f8878cf0 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs @@ -0,0 +1,19 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue34558 : _IssuesUITest +{ + public Issue34558(TestDevice device) : base(device) { } + + public override string Issue => "[Windows] WebView renders blank when HybridWebView and WebView coexist in same app"; + + [Test] + [Category(UITestCategories.WebView)] + public void WebViewLoadsWhenCoexistingWithHybridWebView() + { + App.WaitForElement("HybridStatusLabel"); + } +} From 2fe405c0e7085b38df49e7bafb110ed7016e50b1 Mon Sep 17 00:00:00 2001 From: SubhikshaSf4851 Date: Wed, 22 Apr 2026 17:08:54 +0530 Subject: [PATCH 3/4] Test Sample updated --- .../TestCases.HostApp/Issues/Issue34558.cs | 22 +++++++++++++++++-- .../Tests/Issues/Issue34558.cs | 8 +++++++ .../HybridWebViewHandler.Windows.cs | 10 ++++----- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs index f3f6d0540e96..b9c8cc57ba08 100644 --- a/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs @@ -1,4 +1,4 @@ -namespace TestCases.HostApp.Issues; +namespace Maui.Controls.Sample.Issues; [Issue(IssueTracker.Github, 34558, "[Windows] WebView renders blank when HybridWebView and WebView coexist in same app", PlatformAffected.UWP)] public partial class Issue34558 : ContentPage @@ -29,7 +29,15 @@ public Issue34558() Source = new HtmlWebViewSource { Html = "

WebView should load here

If you see this, the WebView is working.

" }, }; - Content = new VerticalStackLayout + // This label is added to the layout only after the WebView successfully navigates, + // so the UI test can wait for it to appear as proof the WebView rendered content. + var webViewNavigatedLabel = new Label + { + AutomationId = "WebViewNavigatedLabel", + Text = "WebView successfully navigated and rendered content!", + }; + + var layout = new VerticalStackLayout { Spacing = 10, Padding = new Thickness(16), @@ -40,5 +48,15 @@ public Issue34558() webView, } }; + + webView.Navigated += (s, e) => + { + if (e.Result == WebNavigationResult.Success) + { + layout.Children.Add(webViewNavigatedLabel); + } + }; + + Content = layout; } } diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs index 85e4f8878cf0..7342fdc7d4cd 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs @@ -15,5 +15,13 @@ public Issue34558(TestDevice device) : base(device) { } public void WebViewLoadsWhenCoexistingWithHybridWebView() { App.WaitForElement("HybridStatusLabel"); + App.WaitForElement("RegularWebView"); + + var rect = App.WaitForElement("RegularWebView").GetRect(); + Assert.That(rect.Height, Is.GreaterThan(0), "WebView should render with non-zero height"); + + // "WebViewNavigatedLabel" is only added to the layout after the WebView successfully + // navigates. Waiting for it confirms the WebView rendered content and was not blank. + App.WaitForElement("WebViewNavigatedLabel"); } } diff --git a/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs b/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs index 8284632374c1..8c00b7ae41c4 100644 --- a/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs +++ b/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs @@ -314,12 +314,12 @@ private async Task TryInitializeWebView2(WebView2 webView) // data folder to use compatible environments. When no customizations are needed, // use EnsureCoreWebView2Async() so this control joins the default shared environment. bool hasCustomSettings = - initializingArgs.BrowserExecutableFolder != null || - initializingArgs.UserDataFolder != null || - initializingArgs.EnvironmentOptions != null || - initializingArgs.ScriptLocale != null || + !string.IsNullOrEmpty(initializingArgs.BrowserExecutableFolder) || + !string.IsNullOrEmpty(initializingArgs.UserDataFolder) || + !string.IsNullOrEmpty(initializingArgs.EnvironmentOptions) || + !string.IsNullOrEmpty(initializingArgs.ScriptLocale) || initializingArgs.IsInPrivateModeEnabled || - initializingArgs.ProfileName != null; + !string.IsNullOrEmpty(initializingArgs.ProfileName); if (hasCustomSettings) { From 978327e403aacda54a5ab1ef8def76073ca499bd Mon Sep 17 00:00:00 2001 From: SubhikshaSf4851 Date: Wed, 22 Apr 2026 17:08:54 +0530 Subject: [PATCH 4/4] Test Sample updated --- .../TestCases.HostApp/Issues/Issue34558.cs | 22 +++++++++++++++++-- .../Tests/Issues/Issue34558.cs | 8 +++++++ .../HybridWebViewHandler.Windows.cs | 4 ++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs index f3f6d0540e96..b9c8cc57ba08 100644 --- a/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs @@ -1,4 +1,4 @@ -namespace TestCases.HostApp.Issues; +namespace Maui.Controls.Sample.Issues; [Issue(IssueTracker.Github, 34558, "[Windows] WebView renders blank when HybridWebView and WebView coexist in same app", PlatformAffected.UWP)] public partial class Issue34558 : ContentPage @@ -29,7 +29,15 @@ public Issue34558() Source = new HtmlWebViewSource { Html = "

WebView should load here

If you see this, the WebView is working.

" }, }; - Content = new VerticalStackLayout + // This label is added to the layout only after the WebView successfully navigates, + // so the UI test can wait for it to appear as proof the WebView rendered content. + var webViewNavigatedLabel = new Label + { + AutomationId = "WebViewNavigatedLabel", + Text = "WebView successfully navigated and rendered content!", + }; + + var layout = new VerticalStackLayout { Spacing = 10, Padding = new Thickness(16), @@ -40,5 +48,15 @@ public Issue34558() webView, } }; + + webView.Navigated += (s, e) => + { + if (e.Result == WebNavigationResult.Success) + { + layout.Children.Add(webViewNavigatedLabel); + } + }; + + Content = layout; } } diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs index 85e4f8878cf0..7342fdc7d4cd 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34558.cs @@ -15,5 +15,13 @@ public Issue34558(TestDevice device) : base(device) { } public void WebViewLoadsWhenCoexistingWithHybridWebView() { App.WaitForElement("HybridStatusLabel"); + App.WaitForElement("RegularWebView"); + + var rect = App.WaitForElement("RegularWebView").GetRect(); + Assert.That(rect.Height, Is.GreaterThan(0), "WebView should render with non-zero height"); + + // "WebViewNavigatedLabel" is only added to the layout after the WebView successfully + // navigates. Waiting for it confirms the WebView rendered content and was not blank. + App.WaitForElement("WebViewNavigatedLabel"); } } diff --git a/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs b/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs index 8284632374c1..9473b182d6c3 100644 --- a/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs +++ b/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs @@ -317,9 +317,9 @@ private async Task TryInitializeWebView2(WebView2 webView) initializingArgs.BrowserExecutableFolder != null || initializingArgs.UserDataFolder != null || initializingArgs.EnvironmentOptions != null || - initializingArgs.ScriptLocale != null || + !string.IsNullOrEmpty(initializingArgs.ScriptLocale) || initializingArgs.IsInPrivateModeEnabled || - initializingArgs.ProfileName != null; + !string.IsNullOrEmpty(initializingArgs.ProfileName); if (hasCustomSettings) {