简体   繁体   中英

Xamarin Native Webview Android

I have a Webview in my Xamarin Android App but when i want to use Webview.navigate this does not exsist in the event list. I want to execute code when the user navigate. But like I said i have no navigate event.

Please help me.

I was facing something similar and managed to solve it by implementing a custom WebViewClient and by setting the WebView 's WebViewClient using the method : SetWebViewClient . Take this code for example :

public class CustomWebViewClient : WebViewClient
{
    public event EventHandler<bool> OnPageLoaded;

    public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
    {
        view.LoadUrl(request.Url.ToString());
        return true;
    }

    public override void OnPageStarted(WebView view, string url, global::Android.Graphics.Bitmap favicon)
    {
        base.OnPageStarted(view, url, favicon);
    }

    public override void OnPageFinished(WebView view, string url)
    {
        OnPageLoaded?.Invoke(this, true);
    }

    public override void OnReceivedError(WebView view, IWebResourceRequest request, WebResourceError error)
    {
        OnPageLoaded?.Invoke(this, false);
    }
}

And on your activity :

var wbMain = FindViewById<WebView>(Resource.Id.wbMain);
var customWebViewClient = new CustomWebViewClient();

customWebViewClient.OnPageLoaded += CustomWebViewClient_OnPageLoaded;

wbMain.SetWebViewClient(customWebViewClient);
wbMain.LoadUrl("http://the.page.to.load");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM