问题描述
我是用我的项目 web浏览器
控制。
和我注意到,如果用户preSS右键 - >刷新
And i noticed that if the user press right click -> Refresh the
导航
LoadCompleted
不叫。
和另一个奇怪的是,之后我preSS刷新并单击网页浏览器的另一个链接 LoadCompleted
不叫了。
And another strange thing is that after i press the refresh and click another link in the webbrowser the LoadCompleted
not called too.
这是我如何浏览网页的启动:
This is how i navigate the page in the start :
nrowser.Navigate(MYPAGE);
任何想法可能是什么问题?
Any idea what can be the problem?
我可以修复它与的JavaScript
?
推荐答案
您可以处理刷新和导航区别开来。随着一点点的运动,它可以通过处理DOM来完成 document.onreadystatechange
和 window.onunload
活动,以及底层web浏览器ActiveX控件的 NavigateComplete2
和 DownloadBegin
事件。
You can handle a refresh and differentiate it from navigation. With a little exercise, it can be done by handling DOM document.onreadystatechange
and window.onunload
events, as well as the underlying WebBrowser ActiveX control's NavigateComplete2
and DownloadBegin
events.
NavigateComplete2
不被解雇的顶级 web浏览器
对象时,页面被刷新,但它确实导航时。 DownloadBegin
总是被炒鱿鱼,任何下载,浏览或者刷新的活动,可能是多次。 window.onunload
被导航或刷新,而 document.onreadystatechange
被炒鱿鱼之前导航或刷新过程中被解雇。剩下的就是跟踪状态转换的问题。这是一个工作示例(其中仍可能包含bug)。
NavigateComplete2
doesn't get fired for the top WebBrowser
object when the page is being refreshed, but it does when navigating. DownloadBegin
always gets fired, for any download, navigation or refresh activity, possibly multiple times. window.onunload
gets fired before navigation or refresh, and document.onreadystatechange
gets fired during navigation or refresh. The rest is the matter of tracking the state transitions. Here is a working example (which still may contain bugs).
C#:
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
namespace WpfWebBrowser
{
public partial class MainWindow : Window
{
bool navigating = false;
bool loading = false;
bool loaded = false;
public MainWindow()
{
SetBrowserFeatureControl();
InitializeComponent();
this.Loaded += (s, e) =>
{
var axWebBrowser = (SHDocVw.WebBrowser)GetActiveXInstance(this.webBrowser);
axWebBrowser.DownloadBegin += delegate
{
HandleDownloadActivity();
};
axWebBrowser.NavigateComplete2 += delegate(object pDisp, ref object URL)
{
// top frame?
if (Object.ReferenceEquals(axWebBrowser, pDisp))
{
this.navigating = true;
HandleDownloadActivity();
}
};
this.webBrowser.Navigate("http://example.com");
};
}
// handler for document.readyState == "complete"
void DomDocumetCompleteHandler(dynamic domDocument)
{
dynamic domWindow = domDocument.parentWindow;
domWindow.attachEvent("onunload", new DomEventHandler(delegate
{
this.loaded = false;
this.loading = false;
}));
var navigated = this.navigating;
this.navigating = false;
this.loaded = true;
this.loading = false;
MessageBox.Show(navigated ? "Navigated" : "Refreshed");
}
void HandleDownloadActivity()
{
dynamic domDocument = this.webBrowser.Document;
if (domDocument == null)
return;
if (loading || loaded)
return;
this.loading = true;
if (domDocument.readyState == "complete")
{
DomDocumetCompleteHandler(domDocument);
}
else
{
DomEventHandler handler = null;
handler = new DomEventHandler(delegate
{
if (domDocument.readyState == "complete")
{
domDocument.detachEvent("onreadystatechange", handler);
DomDocumetCompleteHandler(domDocument);
}
});
domDocument.attachEvent("onreadystatechange", handler);
}
}
/// <summary>
/// Get the underlying WebBrowser ActiveX object;
/// this code depends on SHDocVw.dll COM interop assembly,
/// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
/// and add as a reference to the project
/// </summary>
static object GetActiveXInstance(WebBrowser wb)
{
return wb.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, wb, new object[] { }) as SHDocVw.WebBrowser;
}
/// <summary>
/// EventHandler - adaptor to call C# back from JavaScript or DOM event handlers
/// </summary>
[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch)]
public class DomEventHandler
{
[ComVisible(false)]
public delegate void Callback(ref object result, object[] args);
[ComVisible(false)]
private Callback _callback;
[DispId(0)]
public object Method(params object[] args)
{
var result = Type.Missing; // Type.Missing is "undefined" in JavaScript
_callback(ref result, args);
return result;
}
public DomEventHandler(Callback callback)
{
_callback = callback;
}
}
/// <summary>
/// WebBrowser version control
/// http://msdn.microsoft.com/en-us/library/ee330730(v=vs.85).aspx#browser_emulation
/// </summary>
void SetBrowserFeatureControl()
{
// http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx
// FeatureControl settings are per-process
var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
// make the control is not running inside Visual Studio Designer
if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0)
return;
// Webpages containing standards-based !DOCTYPE directives are displayed in IE9/IE10 Standards mode.
SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, 9000);
}
void SetBrowserFeatureControlKey(string feature, string appName, uint value)
{
using (var key = Registry.CurrentUser.CreateSubKey(
String.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature),
RegistryKeyPermissionCheck.ReadWriteSubTree))
{
key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
}
}
}
}
XAML:
<Window x:Class="WpfWebBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="800" Height="600">
<WebBrowser Name="webBrowser"/>
</Window>
这篇关于WPF web浏览器刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!