【发布时间】:2017-05-23 16:07:09
【问题描述】:
我正在开发一个 UWP 项目,我想加载一个不透明度 = 0.01f 的 WebView,并且只在调用 WebView_LoadCompleted 时将不透明度设置为 1.0f。在此之前,它由预览图像呈现。但是当我展示它时,它会在 20 个案例中像 1 个一样闪烁白色,并且我正在测试相同的 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 还没有工作后尝试获取调用,我尝试了一些调用,如 WebView 和 ViewBox 的 x_LayoutUpdated、x_SizeChanged,但 WebView_LoadCompleted 是最后一个调用的函数,运行到断点。
将 WebView.DefaultBackgroundColor 设置为透明不起作用,它仍然闪烁白色。 将 WebView 放置在 Image 下方并在布局时间延迟后将其置于前面不起作用。
感谢您的帮助!我很感激!
【问题讨论】:
-
我认为这里的问题是
ViewBox尝试更改您的WebView的容器类型。 -
嗨,我查了一下,但看不到任何可以验证您建议的内容,但我发现了这个问题:stackoverflow.com/questions/34340134/… 有人在 UWP 中为 WPF 函数寻找类似的回调“ContentRendered”,如果在生命周期调用中作为呈现/渲染的最终调用是有效的,那也会对我有所帮助。