2017-02-28 61 views
0

我正在開發一個應用程序Xamarin.UWP,它試圖將Javascript注入到本地html文件(uri:ms-appdata:/// local/index。 HTML)像這樣:是否有腳本通知不適用於UWP ms-appdata的解決方法

async void OnWebViewNavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args) 
{ 
    if (args.IsSuccess) 
    { 
     // Inject JS script 
     if (Control != null && Element != null) 
     { 
      foreach (var f in Element.RegisteredFunctions.Where(ff => !ff.IsInjected)) 
      { 
       await Control.InvokeScriptAsync("eval", new[] { string.Format(JavaScriptFunctionTemplate, f.Name) }); 
       f.Injected(); 
      } 
     } 
    } 
} 

然後,當Javascript方法被調用,這將調用OnWebViewScriptNotify方法,這樣我可以proccess在我的應用程序的請求。

麻煩的是一些kind of security reasons這並不工作:

這是我們做了,我們已經有反饋,所以我們 重新評估它的政策決定。如果您將 NavigateToStreamUri與解析器對象一起使用,則相同的限制不適用。在內部, ms-appdata:///會發生什麼情況。

然後我試了一下在這種情況下,這是使用一個解析器這裏提到建議:https://stackoverflow.com/a/18979635/2987066

但是這會對性能產生巨大的影響,因爲它是所有的文件不斷地轉換成流加載,以及某些頁面加載不正確。

我又看了看使用AddWebAllowedObject方法,像這樣:

private void Control_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) 
{ 
    if (Control != null && Element != null) 
    { 
     foreach (var f in Element.RegisteredFunctions) 
     { 
      var communicator = new HtmlCommunicator(f); 
      Control.AddWebAllowedObject("HtmlCommunicator", communicator); 
     } 
    } 
} 

其中HtmlCommunicator是:

[AllowForWeb] 
public sealed class HtmlCommunicator 
{ 
    public JSFunctionInjection Function { get; set; } 

    public HtmlCommunicator(JSFunctionInjection function) 
    { 
     Function = function; 
    } 

    public void Fred() 
    { 
     var d = 2; 
     //Do something with Function 
    } 
} 

,並在我的HTML它就像這樣:

try { window.HtmlCommunicator.Fred(); } catch (err) { } 

但這也不起作用。

那麼有沒有辦法解決這個荒謬的限制?

+0

從[http://blog.damiendelaire.com/2016/06/communicating-back-and-forth-with.html](http://blog.damiendelaire.com/2016/06/communicating-back- and-forth-with.html)我讀到'因爲一些奇怪的原因,你需要添加在新的WRC庫中創建這個標記,並將這個類標記爲[AllowForWeb]'我將調查更多 – user1

+0

我在上面的鏈接中注意到[此鏈接](https://www.suchan.cz/2016/01/hacking-uwp-webview-part-2-bypassing-window-external-notify-whitelist/)他們都使用'ms-app-web',所以我'我不確定最後一種方法是否適用於'ms-app-data'。將該課程放在另一個WRC庫中不起作用 – user1

回答

0

所以我發現了這個答案:C# class attributes not accessible in Javascript

它說:

我相信你需要定義開始以較低的 字母字符的方法名。

例如:將GetIPAddress更改爲getIPAddress。

我在我身邊進行了測試,發現如果我使用大寫字母名稱 'GetIPAddress',它將不起作用。但是,如果我使用getIPAddress,它會起作用。

所以,我想這一點:

我創建Windows Runtime Component類型的新項目的建議here,我改變了我的方法名稱爲小寫,所以我不得不:

[AllowForWeb] 
public sealed class HtmlCommunicator 
{ 
    public HtmlCommunicator() 
    { 
    } 

    public void fred() 
    { 
     var d = 2; 
     //Do something with Function 
    } 
} 

在我的javascript我然後得到:

try { window.HtmlCommunicator.fred(); } catch (err) { } 

和在我的主要UWP項目我引用了新的Windows Runtime Component圖書館,並有以下內容:

public HtmlCommunicator communicator { get; set; } 

private void Control_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) 
{ 
    if (Control != null && Element != null) 
    { 
     communicator = new HtmlCommunicator(); 
     Control.AddWebAllowedObject("HtmlCommunicator", communicator); 
    } 
} 

這工作!

+0

你可以提供一個小函數,它返回來自HtmlCommunicator類的模型,而不管其爲空。 –

相關問題