sof98789
sof98789

Reputation: 1081

How to load HTML page with javascript content in a WebView Xamarin.Forms?

I am new in xamarin and c# forms platform. I make a simple webview application. and my main page is

namespace jsinjection
{
    public class WebviewPage : ContentPage
    {

        public WebviewPage ()
        {
            Content = new WebView {

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

I want to do javascript injection to that webview according to platform specific data. But I dont know how to get this webview outside of its class. And which part of the project should I do injection.

Upvotes: 0

Views: 5563

Answers (2)

Coder the Great
Coder the Great

Reputation: 143

See if you can use the below code:

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

or

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

See also: Xamarin: How to get HTML from page in WebView?

Upvotes: 2

Andrii Tsok
Andrii Tsok

Reputation: 1446

Take a look to Xamarin.Forms.HtmlWebViewSource

And here is an example:

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

Or using 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"};

Upvotes: 0

Related Questions