0

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.