【问题标题】:How to load HTML page with javascript content in a WebView Xamarin.Forms?如何在 WebView Xamarin.Forms 中加载带有 javascript 内容的 HTML 页面?
【发布时间】:2016-03-21 20:56:35
【问题描述】:

我是 xamarin 和 c# 表单平台的新手。我制作了一个简单的 webview 应用程序。我的主页是

namespace jsinjection
{
    public class WebviewPage : ContentPage
    {

        public WebviewPage ()
        {
            Content = new WebView {

                Source = "https://www.google.com.tr/",
                HeightRequest = 1000,
                WidthRequest = 1000
            };
        }
    }
}

我想根据平台特定的数据对该 webview 进行 javascript 注入。但我不知道如何让这个 webview 在它的类之外。以及我应该在项目的哪个部分进行注入。

【问题讨论】:

    标签: c# xamarin xamarin.forms javascript-injection


    【解决方案1】:

    看看你是否可以使用下面的代码:

    WebView wv = new WebView();
    wv.EvaluateJavascript ("document.body.innerHTML");
    

    wv.InvokeScriptAsync("eval", new string[] { "document.body.innerHTML" });
    

    另请参阅: Xamarin: How to get HTML from page in WebView?

    【讨论】:

      【解决方案2】:

      看看Xamarin.Forms.HtmlWebViewSource

      这是一个例子:

      // Get html content of your page.. 
      // It could contain javascript code or references to files in the assets (example bellow).
      string htmlText = MyItem.Article.ToString();
      
      var browser = new WebView ();
      var html = new HtmlWebViewSource {
        Html = htmlText
      };
      
      // Set your HTML content to implemented WebView
      browser.Source = html;
      

      或使用Xamarin.Forms.UrlWebViewSource

      // Just use local URL for your HTML file that stores inside your application content.
      browser.Source = new UrlWebViewSource {Url = "Content/index.html"};
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-11
        相关资源
        最近更新 更多