WPF Webbrowser C# JavaScript interop
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.getElem开发者_如何转开发entById("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?
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
精彩评论