【问题标题】:Delegate was deprecated in iOS 12.0. Please adopt WKWebView. How do I fix this in Xamarin?委托在 iOS 12.0 中已弃用。请采用 WKWebView。如何在 Xamarin 中解决此问题?
【发布时间】:2019-05-24 10:59:49
【问题描述】:

我正在尝试为 iOS 和 Android 创建自定义 WebView 渲染器。我的主要目标是让 WebView 适合它的 HTML 内容;

谷歌搜索后,我很快意识到这只能通过为 iOS 和 Android 制作自定义渲染器来实现。

我正在使用需要委托的解决方案。您可以查看解决方案here。但是,此解决方案于 2016 年发布,因此我收到此编译时错误消息:“'Delegate' 在 iOS 12.0 中已弃用。不再受支持;请采用 'WKWebView'。

PostWebView.xaml

<WebView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Yoors.Views.Templates.PostWebView" x:Name="WebViewer" BackgroundColor="White" Margin="0, 10, 0, 0" VerticalOptions="FillAndExpand" HeightRequest="1000">
    <WebView.Source>
        <HtmlWebViewSource Html="{Binding Data.Content}" />
    </WebView.Source>
</WebView>

CustomWebViewRenderer.cs

 public class CustomWebViewRenderer : WebViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
            Delegate = new CustomUIWebViewDelegate(this);
        }

    }

CustomUIWebViewDelegate.cs

public class CustomUIWebViewDelegate : UIWebViewDelegate
    {


        CustomWebViewRenderer _webViewRenderer;

        public CustomUIWebViewDelegate(CustomWebViewRenderer webViewRenderer = null)
        {
            _webViewRenderer = _webViewRenderer ?? new CustomWebViewRenderer();
        }

        public override async void LoadingFinished(UIWebView webView)
        {
            var wv = _webViewRenderer.Element as PostWebView;
            if (wv != null)
            {
                await System.Threading.Tasks.Task.Delay(100); // wait here till content is rendered
                wv.HeightRequest = (double)webView.ScrollView.ContentSize.Height;
            }
        }
    }

如何根据我的代码采用WKWebView?

【问题讨论】:

    标签: c# ios xamarin webview wkwebview


    【解决方案1】:

    其实很简单,像这样创建一个自定义 Webview:

    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 CustomRenderer 中执行以下操作:

    [assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
    namespace WKWebView.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.LoadRequest(new NSUrlRequest(new NSUrl(Element.Url)));
            }
        }
    }
    }
    

    【讨论】:

    • 您好,感谢您的回答!但是,我的 WebView 使用预定义的字符串 HTML 而不是 URL 实现了 HTMLwebviewsource。我如何使用 html 字符串来实现这一点?我已经用自定义 webview 类的代码更新了我的问题。
    • 嗯,有时间再更新也没关系
    • 只需将您的 HtmlWebViewSource 分配给您的 Xamarin 表单 WebView 的 Source
    猜你喜欢
    • 2020-08-23
    • 2019-11-29
    • 1970-01-01
    • 2020-03-04
    • 2011-04-10
    • 2018-01-30
    • 1970-01-01
    相关资源
    最近更新 更多