System.Windows.Forms.WebBrowser.Refresh showing previous page

送分小仙女□ 提交于 2019-12-25 00:24:30

问题


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.

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?

According to 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?


回答1:


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.




回答2:


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.

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



来源:https://stackoverflow.com/questions/235700/system-windows-forms-webbrowser-refresh-showing-previous-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!