2

我遇到了 WebView 控件的问题。我正在开发 Windows 8 Metro Style 应用程序,我试图在 WebView 的内容中调用脚本,但它总是返回空字符串。

我也尝试将其设为 "WebView.InvokeScript("eval", new string[] { "document.getElementById('contentDiv').offsetHeight" }) 但它的工作原理相同 - 空字符串。我真的没有猜到这里出了什么问题。

public void Sethtml(string html) {
            var toSet = string.Format(Style, html);
            wv.LoadCompleted += wv_LoadCompleted;
            wv.NavigateToString(toSet);

        }

void wv_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            WebView wv = sender as WebView;
            string height = wv.InvokeScript("heightFun", new string[0]); 
        }
 public static readonly string Style = @"<!DOCTYPE html>
                                               <html>
                                               <head>
                                               <title></title>
                                               <style>
                                               body {{
                                               font-family: Segoe UI; 
                                               }}
                                               </style>
                                               <script type='text/javascript'>
                                                var heightFun = function() {{ 
                                                   return document.getElementById('contentDiv').offsetHeight;
                                                }}
                                               </script>
                                               </head>

                                               <body>
                                                   <div id='contentDiv'>
                                                       {0}
                                                   </div>
                                               </body>";
4

2 回答 2

2

刚刚通过上一个答案的提示解决了这个问题。

确保您的返回值是一个字符串。

于 2013-12-30T17:53:06.023 回答
2

您必须使用window.external.notify来发回一个值。请参阅论坛帖子: http ://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/ac390a6a-d4ad-408c-8b13-93f9694cbabe

于 2012-10-24T08:32:54.087 回答