Skip to content

WebView on Windows Does Not Inherit App Theme #34823

Description

@david-maw

Description

On Android a WebView inherits the theme of the app (which initially defaults to the theme of the system) but on Windows it does not (I suspect it inherits the system theme directly, but I have not checked).

So, on Android 35+ an App that controls its own theme will have that theme reflected in an embedded WebView but on Windows 11 it will not.

Because I have a workaround this a low priority issue for me.

Steps to Reproduce

  1. Clone the test repo
  2. Build and run it on Android 35+ running with the default light theme.
  3. You should see this:
Image
  1. Tap "Switch to web page" and observe the page is black text on a white background.
  2. Go back to the main page and switch the app to dark mode.
  3. Tap "Switch to web page" and observe the page is white text on a black background.
  4. Repeat on Windows - the web page theme will not change when the app theme changes.

Link to public reproduction project repository

https://github.com/david-maw/web-theme.git

Version with bug

10.0.50

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

Windows

Affected platform versions

Windows 11, Android 35+

Did you find any workaround?

Yes, before showing the web page explicitly set the WebView2 theme:

        #if WINDOWS
        if (webView.Handler?.PlatformView is Microsoft.UI.Xaml.Controls.WebView2 wv)
        {
            bool isDark = App.Current?.RequestedTheme == AppTheme.Dark;
            await wv.EnsureCoreWebView2Async();
            wv.CoreWebView2.Profile.PreferredColorScheme =
                isDark
                ? Microsoft.Web.WebView2.Core.CoreWebView2PreferredColorScheme.Dark
                : Microsoft.Web.WebView2.Core.CoreWebView2PreferredColorScheme.Light;
        }
        #endif

This code is in the reproduction code guarded with a compile-time WORKAROUND define.

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions