user2931144
user2931144

Reputation: 177

Xamarin WebViewClient onLoadResource/onPageFinished

Hi im trying to create a WebView application, and i want to create a waiting dialog until the page is loaded.

However i have tried different solutions but i cannot figure out why this is not working when it is working for others on the internet. it fails in Xamarin saying that "}" is missing after this line webview.SetWebViewClient(new WebViewClient() {, even that im sure that i remembered the }); at then end.

it seems that it cannot create something inside the webview.SetWebViewClient().

    webview.SetWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        public void onLoadResource(WebView view, String url) {
            //code to show dialog
        }

        public void onPageFinished(WebView view, String url) {
            //code to dismiss dialog
        }

    });

Upvotes: 2

Views: 5127

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

To add WebViewClient to WebView and show Toast message from shouldOverrideUrlLoading in C# you will need to create a class by extending WebViewClient as:

public class HelloWebViewClient : WebViewClient
{
  public Activity mActivity;
  public HelloWebViewClient(Activity mActivity){
   this.mActivity=mActivity
  }
  public override bool ShouldOverrideUrlLoading (WebView view, string url)
        {
                view.LoadUrl (url);
                Toast.MakeText (mActivity, "Toast Message", 
                                     ToastLength.Long).Show();
                return true;
        }
}

and use HelloWebViewClient class constructor to pass activity Context:

web_view.SetWebViewClient (new HelloWebViewClient (this)); 

Upvotes: 6

Related Questions