2011-09-26 98 views

回答

1

我不知道我是否理解你的問題,但如果我這樣做,你基本上只想顯示加載的網頁,只有當它的渲染完成。 (假設你將「LoadCompleted」事件掛鉤到「webBrowser1_LoadCompleted」方法)。此代碼使用Button(「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; 
    } 

請記住,雖然,沒有顯示任何東西給用戶的時間長週期(如一個沉重的頁面)並不總是一個好主意,這取決於你正在寫的一種應用。不過,這取決於你。

+0

更改頁面時看到閃爍,出現灰色背景(嘗試過各種背景設置)。我希望新渲染的頁面覆蓋前一個沒有閃爍的頁面。也試過可見性但它是一樣的。 – P5music

+0

我看到color =「#A0A0A0」是頁面的背景,然後圖像作爲背景加載 – P5music

0

(我決定離開以前的答案,如果有人需要它)

如果你想,直到出現新的一個離開的前一頁可見的,那麼我想你需要一個Windows DLL。這是我的方式。

在代碼文件的頂部插入這兩個import語句:

using System.Runtime.InteropServices; 
using System.Windows.Interop; 

然後,你需要聲明的DLL函數像這樣(在Window類):

[DllImport("user32")] 
private static extern int LockWindowUpdate (IntPtr hWnd); 

然後,讓我們修改的代碼在前面的回答有點:

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)); 
} 

這應該保留最後LOA直到新頁面完成其渲染;正如你可能想象的那樣,DLL函數通過傳遞其句柄來簡單地鎖定窗口的更新。手柄0將其解鎖。