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

Community
  • 1
  • 1
Seyeong Jeong
  • 10,658
  • 2
  • 28
  • 39
  • 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. – AnthonyWJones Nov 27 '11 at 14:22
  • @AnthonyWJones Yes, I am talking about WP7. Also I edited the question to be a question. Thanks for your comment :) – Seyeong Jeong Nov 28 '11 at 21:24

3 Answers3

1

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

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • +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. – AnthonyWJones Nov 28 '11 at 21:55
  • It's not a HTML5 doc. I didn't declare the doctype: ` `. 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 :( – Seyeong Jeong Nov 30 '11 at 21:39
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

AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152
0

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.

Ku6opr
  • 8,126
  • 2
  • 24
  • 31