WebView渲染闪烁
WebView rendering flickers
我正在处理一个 UWP 项目,我想加载一个不透明度 = 0.01f 的 WebView,并且只在调用 WebView_LoadCompleted 时将不透明度设置为 1.0f。在此之前,它由预览图像呈现。但是当我展示它时,它会在 20 个案例中闪烁白色,并且我正在测试相同的 WebView 内容。
我的代码如下所示:
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.Children.Add(backgroundImage);
grid.SetValue(Grid.RowProperty, slide.Pos.Y);
grid.SetValue(Grid.ColumnProperty, slide.Pos.X);
MainGrid.Children.Add(grid);
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;
grid.Children.Add(viewBox);
我的 WebView_LoadCompleted 看起来像这样:
private void WebView_LoadCompleted(object sender, NavigationEventArgs e)
{
WebView webView = (WebView)sender;
webView.Opacity = 1.0f;
}
我原以为 WebView 只会立即渲染其加载的内容,因为它在 ViewBox 中是可见的(低透明度),而且大多数时候它不会闪烁,但它仍然会出现。
尝试在 WebView_LoadCompleted 之后接听电话还没有成功,我尝试了一些像 x_LayoutUpdated、x_SizeChanged 的 WebView 和 ViewBox 电话,但是 WebView_LoadCompleted 是最后一个遇到断点的调用函数。
将 WebView.DefaultBackgroundColor 设置为透明无效,它仍然闪烁白色。
将 WebView 放置在图像下方并在延迟布局时间后将其置于最前面是行不通的。
感谢您的帮助!我很感激!
似乎没有正确的方法来验证 webview 是否已完成 webview 的所有可能的加载和呈现过程。
但是您可以将另一个问题的答案用于其他 UIElements:
我正在处理一个 UWP 项目,我想加载一个不透明度 = 0.01f 的 WebView,并且只在调用 WebView_LoadCompleted 时将不透明度设置为 1.0f。在此之前,它由预览图像呈现。但是当我展示它时,它会在 20 个案例中闪烁白色,并且我正在测试相同的 WebView 内容。 我的代码如下所示:
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.Children.Add(backgroundImage);
grid.SetValue(Grid.RowProperty, slide.Pos.Y);
grid.SetValue(Grid.ColumnProperty, slide.Pos.X);
MainGrid.Children.Add(grid);
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;
grid.Children.Add(viewBox);
我的 WebView_LoadCompleted 看起来像这样:
private void WebView_LoadCompleted(object sender, NavigationEventArgs e)
{
WebView webView = (WebView)sender;
webView.Opacity = 1.0f;
}
我原以为 WebView 只会立即渲染其加载的内容,因为它在 ViewBox 中是可见的(低透明度),而且大多数时候它不会闪烁,但它仍然会出现。
尝试在 WebView_LoadCompleted 之后接听电话还没有成功,我尝试了一些像 x_LayoutUpdated、x_SizeChanged 的 WebView 和 ViewBox 电话,但是 WebView_LoadCompleted 是最后一个遇到断点的调用函数。
将 WebView.DefaultBackgroundColor 设置为透明无效,它仍然闪烁白色。 将 WebView 放置在图像下方并在延迟布局时间后将其置于最前面是行不通的。
感谢您的帮助!我很感激!
似乎没有正确的方法来验证 webview 是否已完成 webview 的所有可能的加载和呈现过程。 但是您可以将另一个问题的答案用于其他 UIElements: