System.Windows.Forms.WebBrowser.Refresh显示上一页

时间:2022-02-24 21:10:41

My WinForms application has a tab with a System.Windows.Forms.WebBrowser control. There are several controls that set the WebBrowser's .Url property, and when the form repaints it calls the WebBrowser's .Refresh(WebBrowserRefreshOption.Completely) method.

我的WinForms应用程序有一个带有System.Windows.Forms.WebBrowser控件的选项卡。有几个控件设置WebBrowser的.Url属性,当表单重新绘制时,它调用WebBrowser的.Refresh(WebBrowserRefreshOption.Completely)方法。

Occasionally, however, the form gets repainted and the WebBrowser content doesn't change. I break on the .Refresh( ), and the .Url is the previous Url. What's going on?

但是,有时候,表单会重新绘制,WebBrowser内容不会更改。我打破.Refresh(),而.Url是前一个Url。这是怎么回事?

According to MSDN:

根据MSDN:

If you set the value of this property and then immediately retrieve it again, the value retrieved may be different than the value set if the WebBrowser control has not had time to load the new document.

Well isn't that special? Why doesn't .Refresh( ) wait until the control has "had time to load the new document" before it redraws? Is there any way to force this to happen?

那不是特别的吗?为什么没有.Refresh()等到控件在重绘之前“有时间加载新文档”?有没有办法强迫这种情况发生?

2 个解决方案

#1


4  

Unless you're doing something very special, it doesn't make sense to force the WB to repaint itself. Since it is its own control and has its own Handle, it is quite capable of repainting itself whenever it deems necessary. Since you are forcing it to repaint at a moment that's completely out of sync with its ReadyState, getting an "old" url is to be expected.

除非你做一些非常特别的事情,否则强迫WB重绘自己是没有意义的。因为它是它自己的控制并且有它自己的Handle,所以它在它认为必要时能够重新绘制它自己。由于你强迫它在与ReadyState完全不同步的时刻进行重新绘制,因此需要获得一个“旧的”URL。

#2


1  

Try adding an event handler to your code that runs when the "DocumentCompleted" event fires. Then add your refresh code in there. There are some examples of code here in MSDN. If you do it this way you won't lose any time guessing if the page has loaded, and can continue processing as soon as it is ready.

尝试将事件处理程序添加到“Doc​​umentCompleted”事件触发时运行的代码中。然后在那里添加刷新代码。 MSDN中有一些代码示例。如果您这样做,您将不会丢失任何时间猜测页面是否已加载,并且可以在准备好后立即继续处理。

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx

#1


4  

Unless you're doing something very special, it doesn't make sense to force the WB to repaint itself. Since it is its own control and has its own Handle, it is quite capable of repainting itself whenever it deems necessary. Since you are forcing it to repaint at a moment that's completely out of sync with its ReadyState, getting an "old" url is to be expected.

除非你做一些非常特别的事情,否则强迫WB重绘自己是没有意义的。因为它是它自己的控制并且有它自己的Handle,所以它在它认为必要时能够重新绘制它自己。由于你强迫它在与ReadyState完全不同步的时刻进行重新绘制,因此需要获得一个“旧的”URL。

#2


1  

Try adding an event handler to your code that runs when the "DocumentCompleted" event fires. Then add your refresh code in there. There are some examples of code here in MSDN. If you do it this way you won't lose any time guessing if the page has loaded, and can continue processing as soon as it is ready.

尝试将事件处理程序添加到“Doc​​umentCompleted”事件触发时运行的代码中。然后在那里添加刷新代码。 MSDN中有一些代码示例。如果您这样做,您将不会丢失任何时间猜测页面是否已加载,并且可以在准备好后立即继续处理。

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx