Welcome Guest Search | Active Topics | Sign In | Register

Webview Background Color EO2020/EO2021 Options
leo158
Posted: Friday, December 3, 2021 11:15:06 AM
Rank: Newbie
Groups: Member

Joined: 7/5/2017
Posts: 6
I am having an issue that appears to be only happening in EO2021. I used to do something like this in EO2020
string script =
const css = `
html{
background-color: somebackgroundcolorInHex;

}`;
const style = document.createElement('style');
style.textContent = css;

webControl.WebView.JSInitCode = script;

In EO2020, the webview would have its background color correctly set to whatever hex code is provided, but after upgrading to EO2021, this setting appears to be completely ignored, and the webview loads "white" on startup instead of the background color provided. I have also tried something like this:


string styleSheet = "body { backgroud-color: + "someHexColor"+ }";
EO.WebEngine.BrowserOptions options = new EO.WebEngine.BrowserOptions();
options.UserStyleSheet = styleSheet;

but to no effect as well. Is this a known issue introduced in EO2021 or do I need to change the way I am coloring the webview's background?






eo_support
Posted: Monday, December 6, 2021 2:27:28 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

Both options still work in the current version. The key is both are applied very early during the page's life cycle, thus they are very likely to be overriden by styles loaded later in the page. For example, you can use the following code to turn a page's default background color to red:

Code: C#
EO.WebEngine.BrowserOptions options = new EO.WebEngine.BrowserOptions();
options.UserStyleSheet = "body { background-color: red; }";
webView.SetOptions(options);


However these style options are applied before the actual page contents are loaded. So if you have the following HTML in your page:

Code: HTML/ASPX
<style>
body
{
	background-color:green;
}
</style>
<body>
   Am I red?
</body>


Then the text will not be red because the style in the page has overriden this to be green.

The same principle applies when you use JSInitCode to create style element because it is created before the actual page content is loaded.

To resolve this issue you must change when the style is applied. One way to do this is to still use JSInitCode but instead of creating the style element right away, you can delay it until the page has been loaded. For example:

Code: C#
string js = @"
window.addEventListener('load', function()
{
    var style = document.createElement('style');
    style.textContent = 'body { background-color: red; }';
    document.body.appendChild(style);
});";

webView.JSInitCode = js;


This will still run JSInitCode before the page has been loaded. However the code does not create the style element right away. It instead delay the creation until window.onload event. At that point the page contents has already been loaded and now you are free to override whatever styles set by the page, instead of being overriden by it.

Hope this helps. Please feel free to let us know if you still have any questions.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.