How to make UWP WebView2 transparent?

58 views Asked by At

I am trying to find a way to make the UWP WebView2 transparent to use it in game bar widget that supports full transparency.

I tried different ways I found on the Internet but nothing worked. The background stays white.

1

There are 1 answers

0
Junjie Zhu - MSFT On

According to the document, it is recommended to use Environment.SetEnvironmentVariable to set the transparency.

Setting DefaultBackgroundColor

On WinUI 2, the DefaultBackgroundColor property is not exposed directly. You can set the default background color by setting an environment variable, as follows:

Environment.SetEnvironmentVariable("WEBVIEW2_DEFAULT_BACKGROUND_COLOR","FF000000");

Setting transparency

On WinUI 2, transparency is achieved by setting the color to 00FFFFFF.

This environment variable must be set prior to CoreWebView2 creation, which occurs when the WebView2.Source property is initially set or the WebView2.EnsureCoreWebView2Async method is initially called.

Environment.SetEnvironmentVariable("WEBVIEW2_DEFAULT_BACKGROUND_COLOR", "00FFFFFF");
await webView2.EnsureCoreWebView2Async();