2012-10-23 88 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