xamarin - 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;
}
}
}
请帮我,
谢谢
解决方案
推荐阅读
- ios - Xamarin.ios:详细命令为空,因为它显示在函数之前
- javascript - 我可以总是假设不返回 Promise 或 Observable 的函数是 JavaScript 中的同步函数吗?
- google-chrome - 谷歌在谷歌浏览器中阻止了网站/域
- python - Python,将 jpg 文件添加到 zipfile,忽略现有(不要覆盖它)
- node.js - Google Maps Directions API:计算给定点是否在路线上
- java - 在单独的 IDE 中调试 flex 应用程序的 java 部分
- ruby-on-rails - Rails 5 - Shrine 问题:“nil:NilClass 的未定义方法‘cached_image_data’”
- ios - SceneKit move:to action在缩放动作后的一系列动作中不起作用
- css - 如何正确缩放背景图像
- reactjs - ReactJS 和 Redux。如何突出显示当前点击的元素?