4

I'd like to call C# methods within a WebBrowser Control. Below is my code:

In XAML,

<phone:WebBrowser Margin="0,0,0,0" Name="WebBrowserForDetails" VerticalAlignment="Top" Height="300" ScriptNotify="WebBrowserForDetails_ScriptNotify" IsScriptEnabled="True" />

In C#,

protected override void OnNavigatedTo(NavigationEventArgs e)
{

    string html = string.Format("<html><head><title></title><script type=\"text/javascript\">{0}</script></head><body><button onclick=\"call();\">Push</button>", "function call(){ window.external.notify(123) ;}");
    WebBrowserForDetails.NavigateToString(html);
}

private void WebBrowserForDetails_ScriptNotify(Object sender, NotifyEventArgs e)
{
    Debug.WriteLine(e.Value);
}

Expected to see 123 in the debug window.

When the <button>Push</button> is pushed, window.external.notify is never called. In fact, window.external is not available. I would like to call the window.external.notify function from the WebBrowser control to call the WebBrowserForDetails_ScriptNotify method. What should I do?

Edit

Reference links: MSDN: window.external.notify, Any way to set the WP7 Webbrowser control height Dynamically and lock scrolling? and Displaying HTML Content in Windows Phone 7

2
  • 1
    I assume you mean Windows-Phone-7? not Windows-Mobile. For future reference we prefer that the content of a stackoverflow question actually contain a question. Your is a statement of fact. Nov 27, 2011 at 14:22
  • @AnthonyWJones Yes, I am talking about WP7. Also I edited the question to be a question. Thanks for your comment :) Nov 28, 2011 at 21:24

3 Answers 3

1

I suspect that your issue is due to not providing a valid HTML5 document as your (body & html) elements aren't closed.

2
  • +1 For the final point, but you need to read the code in the question more closely, neither of your first two points is true. Nov 28, 2011 at 21:55
  • It's not a HTML5 doc. I didn't declare the doctype: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">. According to HTML4 spec (and HTML5 spec, too) you don't need closing body and html tags. Although I put closing tags, I still window.external gives an empty value :( Nov 30, 2011 at 21:39
0
+50

Your code seems right, but window.external.notify() method is takes String parameter, not Integer. Try window.external.notify('123') call instead. Hope this helps.

4
  • The problem is that window.external returns nothing but empty so I can't even get down to window.external.notify Dec 2, 2011 at 2:54
  • There is test project that works for me: fileserve.com/file/BhVhMAZ/HTML.zip
    – Ku6opr
    Dec 2, 2011 at 8:44
  • File not available anymore... please provide code or new link! Nov 5, 2012 at 9:45
  • Sorry, haven't a copy of this project. The main difference between topic project is to pass a number as a string --> '123'
    – Ku6opr
    Nov 12, 2012 at 11:32
0

First off you probably want to read what Mike Taulty comments on offline browser content.

http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2011/06/22/silverlight-html-and-the-webbrowser-control-for-offline-apps.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.