首页 > 解决方案 > IOS上的Xamarin Forms Webview EvaluateJavaScriptAsync错误

问题描述

我使用在 Android 和 IOS 上运行的 Xamarin Forms 应用程序。我使用带有 html 的 webview,在这个 html 中我在登录时调用了一个 javascript 函数。

在Android上它可以正常工作,但在IOS上它不起作用。

我读过,显然错误是由于从 UIWebview 到 WKWebview 的变化,但到目前为止我不知道如何解决这个错误。

我在 Visual Studio 2019 中的 Windows 上创建了我的应用程序,在模拟器设备上它可以工作,但在物理设备上它不起作用。在这种情况下,在 Iphone 上不起作用。

这是我的代码:

登录页面.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyLogin.Views.LoginPage">
    <ContentPage.Content>
        <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
           
            <Label Text="Sign In" FontAttributes="Bold" FontSize="Title" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="50" />
            <Entry x:Name="emailEntry"
            Grid.Row="2"
            BackgroundColor="#31FFFFFF"           
            Placeholder="Email"
            PlaceholderColor="#3AAAF4" />
            <Entry x:Name="passwordEntry"
            Grid.Row="3"
            Margin="0,0,0,0"
            BackgroundColor="#31FFFFFF"
            IsPassword="True"
            Placeholder="Password"
            PlaceholderColor="#3AAAF4" />
            <Button Text="Login" Clicked="OnLoginButtonClicked" BackgroundColor="#3AAAF4" TextColor="White" CornerRadius="8"  />
            <Label></Label>
            <Label x:Name="messageLabel" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" WidthRequest="50" TextColor="Black" FontAttributes="Bold" />
            <WebView x:Name="webView" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" IsVisible="false" />
        </StackLayout>

    </ContentPage.Content>
</ContentPage>

登录页面.cs

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Plugin.DeviceInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Microsoft.CSharp;
using System.Net;
using System.IO;
using System.Reflection;
using Xamarin.Essentials;
using System.Web;

namespace MyLogin.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public string BlackboxString = "";

        public LoginPage()
        {
            InitializeComponent();

            webView.Source = LoadHTMLFileFromResource();
        }
             
        async void OnLoginButtonClicked(object sender, EventArgs e)
        {
         
            var blackbox = "";
           
            blackbox = await webView.EvaluateJavaScriptAsync($"getBlackbox()");          


            //Code to login
        }

  
      
        private string GetLocalAddress()
        {
            var IpAddress = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();

            if (IpAddress != null)
                return IpAddress.ToString();

            return "Could not locate IP Address";
        }

        HtmlWebViewSource LoadHTMLFileFromResource()
        {
            var source = new HtmlWebViewSource();

            // load HTML embed in PCL
            var assembly = typeof(MainPage).GetTypeInfo().Assembly;
            var stream = assembly.GetManifestResourceStream("mypage.mypagename.html");
            using (var reader = new StreamReader(stream))
            {
                source.Html = reader.ReadToEnd();
            }
            return source;
        }
    }
}

请帮我,

谢谢

标签: xamarinxamarin-forms-4

解决方案


推荐阅读