【发布时间】:2020-05-08 07:55:10
【问题描述】:
我正在使用自定义 Webview 来解决 ITMS-90809: Deprecated API Usage - Apple will stop accepting submissions of apps that use UIWebView APIs. 和解析 HTML data。
我的代码:
MyWebView.cs
using Xamarin.Forms;
namespace CustomWebview
{
public class MyWebView : WebView
{
public static readonly BindableProperty UrlProperty = BindableProperty.Create(
propertyName: "Url",
returnType: typeof(string),
declaringType: typeof(MyWebView),
defaultValue: default(string));
public string Url
{
get { return (string)GetValue(UrlProperty); }
set { SetValue(UrlProperty, value); }
}
}
}
IOS
using CustomWebview;
using CustomWebview.iOS;
using WebKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace CustomWebview.iOS
{
public class MyWebViewRenderer : ViewRenderer<MyWebView, WKWebView>
{
WKWebView _wkWebView;
protected override void OnElementChanged(ElementChangedEventArgs<MyWebView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var config = new WKWebViewConfiguration();
_wkWebView = new WKWebView(Frame, config);
SetNativeControl(_wkWebView);
}
if (e.NewElement != null)
{
Control.LoadHtmlString(Element.Url, null); //use this code instead
//Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Url)));
}
}
}
}
当我打开具有 MyWebView 的页面时,页面重定向到 Main.cs 并显示以下异常。
我已经上传了一个示例项目here。
【问题讨论】:
-
参数
Element.Url好像为null。确保您已在 xaml 中设置它。 -
@LucasZhang-MSFT 我已经硬编码了 HTML 字符串值。在这种情况下,自定义 webview 正在工作。在我的项目中,数据是从 get rest API 加载的。在获取数据之前我得到了异常。在到达以下行时,我的应用程序因此异常而损坏。 var response = await client.GetAsync("rest api");在设置 webview url 值之前,应用程序已损坏。
-
@LucasZhang-MSFT 你能检查一下这个sample project吗?我添加了一个私有 api,因为仅在调用 api 时出现问题。
标签: xamarin.forms webview argumentnullexception