2012-10-23 59 views
1

我遇到了WebView控件的问题。我正在开发Windows 8 Metro Style应用程序,我试图在WebView的内容中调用脚本,但它总是返回空字符串。我尝试使它成为“WebView.InvokeScript(”eval“,new string [] {”document.getElementById('contentDiv')。offsetHeight“}),但它的作用相同 - 空字符串。我真的没有任何猜测什么错在这里WebView.InvokeScript始终返回空字符串

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>"; 

回答

2

通过前面的暗示就解决了这个问题答案。

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

+0

谢谢!我真的把我的脑袋弄糟了。我试图返回一个数字和myNumber.toString()做了伎俩。 – Reinaldo