C# WPF webBrowser:如何在渲染完成后显示到图像
我的UI上有一个webBrowser。我询问是否可能不直接显示,而是通过图像显示,并且我希望仅在收到LoadCompleted事件时更新图像。C# WPF webBrowser:如何在渲染完成后显示到图像,c#,wpf,webbrowser-control,C#,Wpf,Webbrowser Control,我的UI上有一个webBrowser。我询问是否可能不直接显示,而是通过图像显示,并且我希望仅在收到LoadCompleted事件时更新图像。 怎么做?我不确定我是否理解了你的问题,但如果我理解了,你基本上只想在加载的网页渲染完成后才显示它 如果是这样的话,这段代码应该可以做到这一点(我假设您将“LoadCompleted”事件连接到“webBrowser1_LoadCompleted”方法)。此代码使用按钮(“button1”)触发导航,但您可以在任何其他位置使用它 //here is the
怎么做?我不确定我是否理解了你的问题,但如果我理解了,你基本上只想在加载的网页渲染完成后才显示它 如果是这样的话,这段代码应该可以做到这一点(我假设您将“LoadCompleted”事件连接到“webBrowser1_LoadCompleted”方法)。此代码使用按钮(“button1”)触发导航,但您可以在任何其他位置使用它
//here is the code that triggers the navigation: when the button is clicked, I hide the
//webBrowser and then navigate to the page (here I used Google as an example)
private void button1_Click(object sender, RoutedEventArgs e)
{
webBrowser1.Visibility = Visibility.Hidden;
webBrowser1.Navigate(new Uri("http://www.google.it"));
}
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
webBrowser1.Visibility = Visibility.Visible;
}
但是,请记住,长时间不向用户显示任何内容(比如厚页面)并不总是一个好主意,这取决于您编写的应用程序的类型。不过,这取决于你。(如果有人需要,我决定留下前面的答案)
如果您想在新页面出现之前保持上一页可见,那么我认为您需要一个Windows DLL。我会这样做的
在代码文件的顶部,插入以下两条导入语句:
using System.Runtime.InteropServices;
using System.Windows.Interop;
然后您需要像这样声明DLL函数(在Window类中):
然后,让我们稍微修改一下前面答案中的代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
IntPtr handle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
LockWindowUpdate(handle);
webBrowser1.Navigate(new Uri("http://www.google.it"));
}
private void webBrowser1_DocumentCompleted(object sender, NavigationEventArgs e)
{
LockWindowUpdate(new IntPtr(0));
}
这将保持屏幕上最后加载的页面,直到新页面完成渲染;正如您所想象的,DLL函数只是通过传递其句柄来锁定窗口的更新。手柄0将其解锁。我看到更改页面时闪烁,出现灰色背景(尝试了各种背景设置)。我希望新呈现的页面覆盖上一个页面而不闪烁。也尝试了可见性,但它是相同的。我看到color=“#a0”是页面的背景,然后加载一个图像作为背景
private void button1_Click(object sender, RoutedEventArgs e)
{
IntPtr handle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
LockWindowUpdate(handle);
webBrowser1.Navigate(new Uri("http://www.google.it"));
}
private void webBrowser1_DocumentCompleted(object sender, NavigationEventArgs e)
{
LockWindowUpdate(new IntPtr(0));
}