在 WebBrowser 控件中,window.external 不可用

发布于 2024-12-18 00:23:39 字数 1692 浏览 1 评论 0原文

我想在 WebBrowser 控件中调用 C# 方法。下面是我的代码:

在 XAML 中,

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

在 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);
}

预计在调试窗口中看到 123

编辑

参考链接:MSDN:window.external.notify设置 WP7 Webbrowser 控件的任何方法高度动态并锁定滚动?显示 HTML Windows Phone 7 中的内容

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漆黑的白昼 2024-12-25 00:23:39

我怀疑您的问题是由于您的 (body & html) 元素未关闭而未提供有效的 HTML5 文档。

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

热鲨 2024-12-25 00:23:39

您的代码看起来是正确的,但是 window.external.notify() 方法采用 String 参数,而不是 Integer。尝试调用 window.external.notify('123') 。
希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文