Viktor_DE Viktor_DE - 1 year ago 108
C# Question

WebView rendering flickers

I'm working on an UWP project and I want to load a WebView with opacity = 0.01f, and only set opacity to 1.0f when WebView_LoadCompleted is called. Until then it is presented by a preview image. But when I'm showing it, it flickers white in like 1 on 20 cases, and I'm testing the same WebView content.
My code looks like this:

Grid grid = new Grid();
//set previewImage as preview of grid content
Image backgroundImage = new Image();
backgroundImage.Source = new BitmapImage(new Uri(@"ms-appdata:///local/" + "some path"));
grid.SetValue(Grid.RowProperty, slide.Pos.Y);
grid.SetValue(Grid.ColumnProperty, slide.Pos.X);

WebView webView = new WebView();
webView.NavigationStarting += WebView_NavigationStarting;
webView.LoadCompleted += WebView_LoadCompleted;
webView.ScriptNotify += WebView_ScriptNotify;
webView.Opacity = 0.01f;
Uri navigationUri = "some local file Uri");
webView.NavigateToLocalStreamUri(navigationUri, myResolver); // myResolver checks if source is allowed to load
webView.Width = 1024;
webView.Height = 768;
Viewbox viewBox = new Viewbox();
viewBox.Stretch = Stretch.Uniform;
viewBox.Child = webView;

And my WebView_LoadCompleted looks like this:

private void WebView_LoadCompleted(object sender, NavigationEventArgs e)
WebView webView = (WebView)sender;
webView.Opacity = 1.0f;

I was expecting that the WebView will only render its laoded content right away because it is visible (with low opicity) in the ViewBox, and most of the times it is not flickering, but it still appears.

Trying to get a call after WebView_LoadCompleted didn't work yet, I tried some calls like x_LayoutUpdated, x_SizeChanged for the WebView and the ViewBox, but WebView_LoadCompleted is the last called function that runs into a breakpoint.

Setting the WebView.DefaultBackgroundColor to transparent didn't work, it still flickers white.
Placing the WebView below the Image and getting it to the front after a delay for layout time didn't work.

Thanks for your help! I appreciate it!

Answer Source

It looks like there is no proper way to validate that the webview is finished with all possible loading and rendering processes for the webview. But you can use this answer of another question for other UIElements:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download