微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 webview 中的 xamarin android 按钮显示警报功能

如何解决如何从 webview 中的 xamarin android 按钮显示警报功能

  • 如何从 webview 中的 xamarin android 按钮显示警报功能
  • 我想在用户单击 Xamarin android 按钮时在 webview 中添加警报。

解决方法

enter image description here 为您的 webview 实现 webviewclient 对象并像这样在 webview.EvaluateJavascript 中调用 OnPageFinished..

//create an internal webviewclient class
 internal class webViewClient : WebViewClient
    {
        public override void OnPageStarted(WebView view,string url,Bitmap favicon)
        {
            base.OnPageStarted(view,url,favicon);
        }

        public override void OnPageFinished(WebView view,string url)
        {
            base.OnPageFinished(view,url);
              //define the javascript code you wish to run
            string script = "javascript:alert('Hi');";
            //perform check and then call
            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
            {
                view.EvaluateJavascript(script,null);
            }
            else
            {
                view.LoadUrl(script);
            }
        }
    }

然后在 webviewOnCreate 中的 buttonclick evet handler 代码中执行此操作

 protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.webview);
            mywebview = FindViewById<WebView>(Resource.Id.mywebiview);
            //This webviewchromeclient is very important
            mywebview.SetWebChromeClient(new WebChromeClient());
            WebSettings webSettings = mywebview.Settings;
            //set javascript enabled for your webview
            webSettings.JavaScriptEnabled = true;
            webSettings.JavaScriptCanOpenWindowsAutomatically = true;
             //set the object we defined above as the webviewclient for your webview
            mywebview.SetWebViewClient(new webViewClient());
         }
,

我试过了,但没有用 =>webView.EvaluateJavascript("javascript: alert('Hi');",null);

您可以尝试通过 webview1.SetWebChromeClient(new WebChromeClient());

使用 WebChromeClient()
  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this,savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
      
        webview1 = FindViewById<WebView>(Resource.Id.webView1);
        button1 = FindViewById<Button>(Resource.Id.button1);

        button1.Click += Button1_Click;

        WebSettings settings = webview1.Settings;
        settings.JavaScriptEnabled = true;
        
        webview1.SetWebChromeClient(new WebChromeClient());        
        webview1.LoadUrl("file:///android_asset/login.html");
   
    }

    private void Button1_Click(object sender,System.EventArgs e)
    {
        //webview1.EvaluateJavascript("javascript: check();",new EvaluateBack());
        webview1.EvaluateJavascript("javascript: alert('Hi');",null);
    }

截图:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。