I have a webBrowser o开发者_开发问答n my UI. I ask if it is possible that it is not displayed directly but instead through an image, and I want that the image is updated only when the LoadCompleted event is received. How to do?
I'm not sure if I understood your question, but if I did, you basically want to show the loaded web page only when its rendering has finished.
If so, this code should do the trick (I'm assuming you hooked the "LoadCompleted" event up to the "webBrowser1_LoadCompleted" method). This code uses a Button ("button1") to trigger the navigation, but you can use it in any other place.
//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;
}
Keep in mind, though, that not showing anything to the user for a long period of time (as with a heavy page) is not always a good idea, depending on the kind of application you're writing. This is up to you, though.
(I decided to leave the previous answer if someone needs it)
If you want to leave the previous page visible until the new one appears, then I think you need a Windows DLL. This is how I would do it.
At the top of the code file, insert these two import statements:
using System.Runtime.InteropServices;
using System.Windows.Interop;
Then you need to declare your DLL function like this (in the Window class):
[DllImport("user32")]
private static extern int LockWindowUpdate (IntPtr hWnd);
Then, let's modify the code in the previous answer a little bit:
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));
}
This should keep the last loaded page on screen until the new page has completed its rendering; as you may imagine, the DLL function simply locks the update of the Window by passing its handle. The handle 0 unlocks it.
精彩评论