WPF Web 浏览器 C# JavaScript 互操作
我正在开发一个应用程序,该应用程序大部分是 html/javascript (整个“HTML5”事物),带有 WPF 应用程序的轻外壳,以便它可以与本地资源交互。
该应用程序有一个 HTML 文件,我以编程方式导航到该文件。加载文件后,我尝试附加事件处理程序,以便可以处理 C# 中的特定事件。
当我尝试使用 mshtml 对象时,似乎我只得到空值。例如,下面是一些代码:
string initUrl = "file:///" + Path.Combine(Environment.CurrentDirectory, Path.Combine("Pages", "ComposeSurface.html"));
navBrowser.Navigate(new Uri(initUrl));
navBrowser.Navigated += (o, e2) =>
{
HTMLDocumentClass documentObject = navBrowser.Document as HTMLDocumentClass;
IHTMLElement ele = documentObject.getElementById("initButton");
if (ele != null)
{
Func<IHTMLEventObj, bool> evra = (arg) => { MessageBox.Show("Hello"); return true; };
ele.onclick = evra as object;
}
};
然而,getElementById 似乎总是返回 null。我已经仔细检查过,元素的 id 是一致的。
我的第二个问题与处理事件有关 - 只要附加一些 C# 到它,这是否适用于来自常规 HTML 表单按钮的单击事件?
I am working on an application that is for the most part html/javascript (the whole "HTML5" thing) with a light shell of a WPF application so that it can interact with local resources.
The application has a single HTML file which I navigate to programmatically. Once the file is loaded, I am trying to attach event handlers so I can handle specific events in C#.
When I try to use the mshtml objects, it appears that I only get nulls back. For example, here is some code:
string initUrl = "file:///" + Path.Combine(Environment.CurrentDirectory, Path.Combine("Pages", "ComposeSurface.html"));
navBrowser.Navigate(new Uri(initUrl));
navBrowser.Navigated += (o, e2) =>
{
HTMLDocumentClass documentObject = navBrowser.Document as HTMLDocumentClass;
IHTMLElement ele = documentObject.getElementById("initButton");
if (ele != null)
{
Func<IHTMLEventObj, bool> evra = (arg) => { MessageBox.Show("Hello"); return true; };
ele.onclick = evra as object;
}
};
getElementById always seems to return a null, however. I've double checked and the id of the element is consistent.
My second question has to do with handling the event - will this work for a click event from a regular HTML form button as far as attaching some C# to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 LoadCompleted 事件。在导航事件期间,该文档仍然可以下载。
http://msdn.microsoft.com/en -us/library/system.windows.controls.webbrowser.naviated.aspx
http://msdn.microsoft.com/en-us /library/system.windows.controls.webbrowser.loadcompleted.aspx
Try using the LoadCompleted event. The document could still be downloading during the Navigated event.
http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.navigated.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.loadcompleted.aspx