Javascript在webview xamarin android中无法启用

pqwbnv8z  于 2023-02-27  发布在  Java
关注(0)|答案(1)|浏览(169)

我创建了一个xamarin android应用程序,我想在webview中显示一个幻灯片,为此我创建了Html变量,在html中我使用了css和javascript。当我浏览它时,它在chrome浏览器中工作,但在应用程序中,javascript不工作。
创建(已保存示例状态);

// Create your application here
            SetContentView(Resource.Layout.WebView);

            webView = FindViewById<WebView>(Resource.Id.webView);
            webView.SetWebChromeClient(new WebChromeClient());
            webView.SetWebViewClient(new WebViewClient());
            webView.ClearCache(true);
            webView.ClearHistory();
            webView.Settings.JavaScriptEnabled = true;
            webView.Settings.JavaScriptCanOpenWindowsAutomatically = true;

//由于安全原因无法提供html代码。webView.LoadData(html,“text/html”,null);

flvlnr44

flvlnr441#

首先,请确保您已将此代码添加到AndroidManifest.xml中以获得互联网权限。

<uses-permission Android:name="Android.permission.INTERNET" />

然后你可以试着用这个代码webView.Settings.DomStorageEnabled = true来设置这个属性的值,默认值是false。
有关详细信息,可以参考有关JavaScript not working in Android Webview的案例

    • 更新**

我已经测试了你的最新问题的编码,发现html不会呈现在我的webview。
然后,我将您的html字符串更改为html文件,并将其放入Assets文件夹

<!DOCTYPE html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    * {box-sizing: border-box;}
    body {font-family: Verdana, sans-serif;}
    .mySlides {display: none;}
    img {vertical-align: middle;}

    /* Slideshow container */
    .slideshow-container {
      max-width: 100%;
      position: relative;
      margin: auto;
    }

    /* Number text (1/3 etc) */
    .numbertext {
      color: #f2f2f2;
      font-size: 12px;
      padding: 8px 12px;
      position: absolute;
      top: 0;
    }

    /* The dots/bullets/indicators */
    .dot {
      height: 15px;
      width: 15px;
      margin: 0 2px;
      background-color: #bbb;
      border-radius: 50%;
      display: inline-block;
      transition: background-color 0.6s ease;
    }

    .active {
      background-color: #717171;
    }

    /* Fading animation */
    .fade {
      animation-name: fade;
      animation-duration: 1.5s;
    }

    @keyframes fade {
      from {opacity: .4}
      to {opacity: 1}
    }

    </style>
</head>

<body>
<div class="slideshow-container">

<div class=mySlides fade>
  <div class=numbertext">1 / 3</div>
  <img src="test.jpg" style="width:100%">  
</div>

<div class="mySlides fade">
  <div class="numbertext">2 / 3</div>
  <img src="test11.png" style="width:100%">  
</div>

<div class="mySlides fade>
  <div class="numbertext">3 / 3</div>
  <img src="test22.png" style="width:100%">  
</div>

</div>
<br>

<div style="text-align:center">
  <span class="dot"></span>
  <span class="dot"></span>
  <span class="dot"></span>
</div>

<script type="text/javascript">
    let slideIndex = 0;
    showSlides();

    function showSlides() {
        let i;
        let slides = document.getElementsByClassName("mySlides");
        let dots = document.getElementsByClassName("dot");
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";
        }
        slideIndex++;
        if (slideIndex > slides.length) { slideIndex = 1 }
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace("active", "");
        }
        slides[slideIndex - 1].style.display = "block";
        dots[slideIndex - 1].className += "active";
        setTimeout(showSlides, 2000); // Change image every 2 seconds
    }
</script>
</body>
</html>

项目图片:

然后使用webView.LoadUrl("file:///android_asset/file.html");加载html文件。
此外,在您的代码中,if (!string.IsNullOrEmpty(NonMeetingLeftPath))应为if (string.IsNullOrEmpty(NonMeetingLeftPath)),因为NonMeetingLeftPath为空。将不执行webView.Load。

相关问题