0

I'm trying to load a web page into a xamarin app my mainactivity.cs page has the following code

public class MainActivity : Activity
{
    WebView webView;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        webView = FindViewById<WebView>(Resource.Id.webView);
        webView.LoadUrl("https://urlofwebpage");

        WebSettings webSettings = webView.Settings;
        webSettings.JavaScriptEnabled = true;
    }
}

and my main.axml contains this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <android.webkit.WebView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webView" />
</LinearLayout>

it seems to build but I only ever get a black screen in the emulator

Can anyone shed some light on why?

andrew slaughter
  • 1,069
  • 5
  • 19
  • 34

1 Answers1

0

First of all, for your code:

    WebSettings webSettings = webView.Settings;
    webSettings.JavaScriptEnabled = true;

The true value is not set to the webView.Settings, it is set to the variable webSettings, they're different object. To enable JavaScriptEnabled of your WebView, directly code like this:

webView.Settings.JavaScriptEnabled = true;

I noticed that your url is like https://urlofwebpage, for ssl connection, you can create a WebViewClient for your WebView, and then override the OnReceivedSslError to check the error message, for example:

webView.SetWebViewClient(new MyWebViewClient());

MyWebViewClient is like this:

public class MyWebViewClient : WebViewClient
{
    public override void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
    {
        base.OnReceivedSslError(view, handler, error);
        switch (error.PrimaryError)
        {
            case SslErrorType.Untrusted:
                //TODO:
                break;

            case SslErrorType.Expired:
                //TODO:
                break;

            case SslErrorType.Idmismatch:
                //TODO:
                break;

            case SslErrorType.Notyetvalid:
                //TODO:
                break;
        }
    }
}
Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • I've followed a couple of tutorials this one https://developer.xamarin.com/recipes/android/controls/webview/load_a_web_page/ and this one https://www.youtube.com/watch?v=YwWOAWmNMXQ and it seems correct the way it is – andrew slaughter Aug 17 '17 at 10:10