2017-04-07 2 views
0

Ich arbeite an Xamarin Formen IOS-Anwendung Aufruf von HTML-Seiten, Stile und Skripte mit Hybrid WebView. Ich bin Stil und Skripte nicht in der Lage zu machen und mein CodeXamarin Forms - IOS, Skripte und Stile rufen nicht

HybridWebViewRenderer.cs

protected override void OnElementChanged (ElementChangedEventArgs<HybridWebView> e) 
    { 
     base.OnElementChanged (e); 

     if (Control == null) { 
      userController = new WKUserContentController(); 
      var script = new WKUserScript (new NSString (JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); 
      userController.AddUserScript (script); 
      userController.AddScriptMessageHandler (this, "invokeAction"); 

      var config = new WKWebViewConfiguration { UserContentController = userController }; 
      var webView = new WKWebView (Frame, config); 
      SetNativeControl (webView); 
     } 

     if (e.OldElement != null) { 
      userController.RemoveAllUserScripts(); 
      userController.RemoveScriptMessageHandler ("invokeAction"); 
      var hybridWebView = e.OldElement as HybridWebView; 
      hybridWebView.Cleanup(); 
     } 

     if (e.NewElement != null) { 
      string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", Element.Uri)); 

      //fileName = "/var/containers/Bundle/Application/2DC89563-D118-4092-B7A5-4549AF07F3B2/StoneApplication.iOS.app/Content/index.html" 

      Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false))); 
     } 
    } 

    public void DidReceiveScriptMessage (WKUserContentController userContentController, WKScriptMessage message) 
    { 
     Element.InvokeAction (message.Body.ToString()); 
    } 

und meine index.html, hier ist Skripte nenne, Bilder und Stile

<html> 
<head> 
<title>STONEAPP TEMPLATE</title> 
<!--Scripts--> 
<script src="/Scripts/angular.min.js"></script> 
<script src="/Scripts/jquery.min.js"></script> 
<script src="/Scripts/bootstrap.js"></script> 
<script src="/Scripts/fileServer.js"></script> 
<script src="/Scripts/fileupload.js"></script> 
<script src="/Scripts/ng-cordova.js"></script> 
<script src="/Scripts/app.js"></script> 

<!--Styles--> 
<link href="/Styles/bootstrap.css" rel="stylesheet"/> 
<link href="/Styles/main.css" rel="stylesheet"/> 
</head> 
<body ng-app="stoneApp" style="background-color: #ffffff;"> 
<div ng-controller="templateCtrl"> 
    <div id="Header" style="background-color:#293846;min-height:50px"> 
     <table style="width:100%;padding-top:5px;padding-left:5px;padding-right:5px"> 
      <tr> 
       <td align="center" width="5%" valign="middle" ng-if="jobView == true" ng-click="showActList()"> 
        <img src="/Images/arrow-back.png" alt="" style="height:50px;width:50px"/> 
       </td> 
       <td align="center" width="5%" valign="middle"> 
        <img src="/Images/wifi_on.png" alt="" style="height:50px;width:50px" ng-click="ShowWifiSettings()"/> 
       </td> 
       <td align="center" width="5%" valign="middle" ng-click="ShowTabSettings()"> 
        <img src="/Images/settings.png" alt="" style="height:50px;width:50px" ng-if="loginView == false"/> 
       </td> 
       <td width="5%" valign="middle" ng-if="activityView == true"></td> 
       <td width="60%" valign="middle"></td> 
       <td width="20%" valign="middle" align="right"> 
        <span style="color:white" ng-if="loginView == false">{{userName}} !</span> 
       </td> 
       <td width="5%" align="right" valign="middle" ng-click="logout()" > 
        <img src="/Images/power_button.png" style="height:50px;width:50px" ng-if="loginView == false"/> 
       </td> 
      </tr> 
     </table> 
    </div> 
    </div> 
    </body> 

I habe Build Action als Bundle-Ressource für Styles und Skripte gegeben, obwohl ich Styles und Skripte nicht aufrufen kann und die Programmstruktur als angehängtes Image ist.

enter image description here

Ich bin mit ios 10.2 Tablette. Also, bitte schauen Sie sich mein Problem an und helfen Sie mir, die Skripte Stile und Bilder wie die Links auf der HTML-Seite aufzurufen. Danke im Voraus.

Antwort

0

Sie können nicht tun, mit Control.LoadRequest

Sie Problem sind, ist, dass Sie nicht baseUrl für Ihre Webansicht mit dieser Methode und keine andere Methode können Sie es definieren definieren können. Wenn Ihre index.html in die Webansicht geladen wird, kann die Webansicht kein Skript oder CSS abrufen, da die baseUrl nicht definiert ist und nicht weiß, wo sie geladen werden soll.

Eine Abhilfe ist LoadHtmlString (String htmlContent, NSUrl baseUrl)

zu verwenden, wo htmlContent Ihre index.html Inhalt in eine var und baseUrl NSUrl baseurl = new NSUrl(NSBundle.MainBundle.BundlePath, true);

+0

Hallo @Leze ich so hinzufügen, aber kein Glück scripts/css/Bilder sind nicht geladen wird loading string htmlPath = Pfad.Kombinieren (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", Element.Uri)); Zeichenfolge htmlContents = File.ReadAllText (htmlPath); Control.LoadHtmlString (htmlContents, null); –

+0

Sie fehlen den BaseUrl-Parameter in Ihrem LoadHtmlString. Versuchen Sie es wie folgt zu tun: string htmlPath = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", Element.Uri)); NSUrl baseurl = new NSUrl (NSBundle.MainBundle.BundlePath, true); Zeichenfolge htmlContents = File.ReadAllText (htmlPath); Control.LoadHtmlString (htmlContents, baseurl); ' – Leze