简体   繁体   中英

how to solve window.close() not work in Android webview c#

i just new using this visual studio c# android...

have system develop in vb.net... working fine in website... problem when wanted using mobile app... used this vs c# xamarin android....

protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle);

    SetContentView(Resource.Layout.Main);

    WebView localWebView = FindViewById<WebView>(Resource.Id.LocalWebView);
    localWebView.SetWebViewClient(new WebViewClient()); // stops request going to Web Browser
    localWebView.Settings.JavaScriptEnabled = true;
    localWebView.Settings.JavaScriptCanOpenWindowsAutomatically = true;
    localWebView.SetWebChromeClient(new WebChromeClient());
    localWebView.LoadUrl("http://www.facebook.com");

}

success run on this webview.... but have form have window.open....problem is how to window.close after used javascript to opened it.. means going back to my previous window.. and pass some information.... like: window.opener.document.getElementById(StrCtrlName2).value = MemberCode;

find and want to try this code... but look different for c#... how to convert it in visual studio c# because have error??... help me.. where to pun also i'm not sure... just learn c#

WebChromeClient webClient = new WebChromeClient(){

    public void onCloseWindow(Window w){
        super.onCloseWindow(w);
        Log.d(TAG, "Window close");
    }
};

thanks...

Welcome @haris!

The code you found is java. It's different from c# but also have a lot similarities.

If you want this particular java snippet in c# you would have do write something like this:

    using Android.Webkit;
    public class CustomWebChromeClient : WebChromeClient { 

        public override void OnCloseWindow(Android.Webkit.WebView window)
        {
            base.OnCloseWindow(window);
            //Your favorite logging library call.
        }

    }

A quick explanation (in case you are interested).

In java snippet a ref to anonymous class which extends WebChromeClient is created and then we extend base onCloseWindow method by adding addition logging.

In c# the same can not be done so what I did, just created a named class CustomWebChromeClient which extends WebChromeClient and overrides OnCloseWindow

For more info refer to official Xamarin docs .

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