Xamarin.forms Xamarin表单:自定义webview在IOS中不起作用

Xamarin.forms Xamarin表单:自定义webview在IOS中不起作用,xamarin.forms,webview,argumentnullexception,Xamarin.forms,Webview,Argumentnullexception,我正在使用自定义Webview解决ITMS-90809:不推荐的API使用-苹果将停止接受使用UIWebView API的应用提交。和解析 我的代码: MyWebView.cs using Xamarin.Forms; namespace CustomWebview { public class MyWebView : WebView { public static readonly BindableProperty UrlProperty = Bindable

我正在使用自定义Webview解决
ITMS-90809:不推荐的API使用-苹果将停止接受使用UIWebView API的应用提交。
和解析

我的代码:

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)));
            }
        }
    }
}
使用CustomWebview;
使用CustomWebview.iOS;
使用WebKit;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.iOS;
[程序集:ExportRenderer(typeof(MyWebView)、typeof(MyWebViewRenderer))]
命名空间CustomWebview.iOS
{
公共类MyWebViewRenderer:ViewRenderer
{
WKWebView WKWebView;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(Control==null)
{
var config=新的WKWebViewConfiguration();
_wkWebView=新的wkWebView(框架,配置);
SetNativeControl(_wkWebView);
}
if(例如NewElement!=null)
{
Control.LoadHtmlString(Element.Url,null);//改用此代码
//LoadRequest(新的NSUrlRequest(新的NSUrl(Element.Url));
}
}
}
}
当我打开使用MyWebView的页面时,页面重定向到Main.cs并显示以下异常


我上传了一个示例项目

我在Forms项目中发现一个.Net Framework库,请删除它并使用.Net标准HttpClient。 其次,尝试禁用特定URL的ATS,如:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>services.catholicbrain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>
</dict>

这个答案来自我的另一位。

参数
Element.Url
似乎为空。确保已在xaml中设置它。@LucasZhang MSFT我已硬编码HTML字符串值。在这种情况下,自定义webview正在工作。在我的项目中,数据是从getrestapi加载的。我在获取数据之前获取异常。到达以下行时,我的应用程序因此异常而中断。var response=await client.GetAsync(“RESTAPI”);在设置webview url值之前,应用程序已损坏。@LucasZhang MSFT您能检查一下吗?我添加了一个私有api,因为只有在调用api时才会出现问题。
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged(sender, e);

    if (e.PropertyName == "Url")
    {
        Control.LoadHtmlString(Element.Url, null);
    }
}