|
| 1 | +CoreWebView2ControllerOptions.DefaultBackgroundColor |
| 2 | +=== |
| 3 | +# Background |
| 4 | +Previously, there was a fix to address an issue where the background color controller property |
| 5 | +was applied too late, causing a disruptive white flash during the WebView2 loading process. |
| 6 | + |
| 7 | +This fix required using an environment variable. However, this workaround was not meant to be |
| 8 | +a long-term solution. Therefore, we need to add this setting to `ICoreWebView2ControllerOptions` |
| 9 | +to apply the color early in the loading process. |
| 10 | + |
| 11 | +# Description |
| 12 | + |
| 13 | +This interface extends the `ICoreWebView2ControllerOptions` to expose the `DefaultBackgroundColor` |
| 14 | +property as an option. |
| 15 | +The `CoreWebView2ControllerOptions.DefaultBackgroundColor` API allows users to set the |
| 16 | +`DefaultBackgroundColor` property at initialization. |
| 17 | + |
| 18 | +This is useful when setting it using the existing [`CoreWebView2Controller.DefaultBackgroundColor API`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2controller.defaultbackgroundcolor?view=webview2-dotnet-1.0.2792.45) |
| 19 | +applies the color too late. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +# Examples |
| 25 | + |
| 26 | +## Win32 C++ |
| 27 | +```cpp |
| 28 | + HRESULT AppWindow::CreateControllerWithOptions() |
| 29 | +{ |
| 30 | + wil::com_ptr<ICoreWebView2ControllerOptions> options; |
| 31 | + HRESULT hr = m_environment->CreateCoreWebView2ControllerOptions(&options); |
| 32 | + |
| 33 | + wil::com_ptr<ICoreWebView2ControllerOptions> stagingOptions; |
| 34 | + auto result = options->QueryInterface(IID_PPV_ARGS(&stagingOptions)); |
| 35 | + |
| 36 | + if (SUCCEEDED(result)) |
| 37 | + { |
| 38 | + COREWEBVIEW2_COLOR wvColor{255, 223, 225, 225}; |
| 39 | + stagingOptions->put_DefaultBackgroundColor(wvColor); |
| 40 | + |
| 41 | + m_environment->CreateCoreWebView2Controller( |
| 42 | + m_mainWindow, options.Get(), |
| 43 | + Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>( |
| 44 | + this, &AppWindow::OnCreateCoreWebView2ControllerCompleted).Get()); |
| 45 | + } |
| 46 | + |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +## C# |
| 52 | +```c# |
| 53 | +public MainWindow() |
| 54 | +{ |
| 55 | +InitializeComponent(); |
| 56 | +SetDefaultBackgroundColor(); |
| 57 | +} |
| 58 | + |
| 59 | +private void SetDefaultBackgroundColor() |
| 60 | +{ |
| 61 | + CoreWebView2ControllerOptions options = WebView2.CoreWebView2.Environment.CreateCoreWebView2ControllerOptions(); |
| 62 | + options.DefaultBackgroundColor = Color.FromArgb(0, 0, 255); |
| 63 | + WebView2.CoreWebView2.Environment.CreateCoreWebView2ControllerAsync(parentHwnd, options); |
| 64 | +} |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +# API Details |
| 73 | + |
| 74 | +## Win32 C++ |
| 75 | + ```cpp |
| 76 | +/// This interface extends the ICoreWebView2ControllerOptions interface to expose the DefaultBackgroundColor property. |
| 77 | +/// It is encouraged to transition away from the environment variable and use this API solution to apply the property. |
| 78 | + |
| 79 | +[uuid(df9cb70b-8d87-5bca-ae4b-6f23285e8d94), object, pointer_default(unique)] |
| 80 | +interface ICoreWebView2ControllerOptions4 : IUnknown { |
| 81 | + |
| 82 | + /// This API allows users to initialize the `DefaultBackgroundColor` early, |
| 83 | + /// preventing a white flash that can happen while WebView2 is loading when |
| 84 | + /// the background color is set to something other than white. With early |
| 85 | + /// initialization, the color remains consistent from the start. After |
| 86 | + /// initialization, `ICoreWebView2Controller2::get_DefaultBackgroundColor` |
| 87 | + /// will return the value set using this API. |
| 88 | + /// |
| 89 | + /// The `DefaultBackgroundColor` is the color that renders underneath all web |
| 90 | + /// content. This means WebView renders this color when there is no web |
| 91 | + /// content loaded. It is important to note that the default color is white. |
| 92 | + /// Currently this API only supports opaque colors and transparency. It will |
| 93 | + /// fail for colors with alpha values that don't equal 0 or 255 ie. translucent |
| 94 | + /// colors are not supported. It also does not support transparency on Windows 7. |
| 95 | + /// On Windows 7, setting DefaultBackgroundColor to a Color with an Alpha value |
| 96 | + /// other than 255 will result in failure. On any OS above Win7, choosing a |
| 97 | + /// transparent color will result in showing hosting app content. This means |
| 98 | + /// webpages without explicit background properties defined will render web |
| 99 | + /// content over hosting app content. |
| 100 | + |
| 101 | + [propget] HRESULT DefaultBackgroundColor([out, retval] COREWEBVIEW2_COLOR* value); |
| 102 | + [propput] HRESULT DefaultBackgroundColor([in] COREWEBVIEW2_COLOR value); |
| 103 | + |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## .NET WinRT |
| 108 | + |
| 109 | +```cpp |
| 110 | +namespace Microsoft.Web.WebView2.Core |
| 111 | +{ |
| 112 | + runtimeclass CoreWebView2ControllerOptions |
| 113 | + { |
| 114 | + // ... |
| 115 | + [interface_name("ICoreWebView2ControllerOptions4")] |
| 116 | + { |
| 117 | + Windows.UI.Color DefaultBackgroundColor { get; set; }; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +``` |
0 commit comments