简体   繁体   English

在iOS 12.0中不推荐使用Delegate。 请采用WKWebView。 如何在Xamarin中解决此问题?

[英]Delegate was deprecated in iOS 12.0. Please adopt WKWebView. How do I fix this in Xamarin?

I am trying to create a Custom WebView Renderer for iOS and Android. 我正在尝试为iOS和Android创建自定义WebView渲染器。 My main goal is to make the WebView fit it's HTML contents; 我的主要目标是使WebView适合其HTML内容。

After googling, I soon realized that this is only possible by making custom renderers for both iOS and Android. 谷歌搜索之后,我很快意识到这只有通过为iOS和Android制作自定义渲染器才能实现。

I am using a solution that requires a delegate. 我正在使用需要委托的解决方案。 You can view the solution here . 您可以在此处查看解决方案。 However, this solution was posted in 2016 and therefore I get this compile time error message: "'Delegate' was deprecated in iOS 12.0. No longer supported; please adopt 'WKWebView'. 但是,此解决方案发布于2016年,因此我收到此编译时错误消息:iOS 12.0中已弃用“ Delegate”。不再受支持;请采用“ WKWebView”。

PostWebView.xaml 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 CustomWebViewRenderer.cs

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

    }

CustomUIWebViewDelegate.cs 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;
            }
        }
    }

How do I adopt WKWebView according to my code? 如何根据我的代码采用WKWebView?

Its quite easy actually, Create a Custom Webview something like this: 实际上,创建一个自定义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); }
 }
}

Then in your iOS CustomRenderer do something like this: 然后在您的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)));
        }
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免在Xamarin iOS中调用不赞成使用的方法? - How to avoid that a deprecated method is called in Xamarin iOS? 如何使用 CustomWebViewRenderer 使 WKWebView 的高度适合其在 Xamarin 中的内容? - How do I make WKWebView's height fit its contents in Xamarin with a CustomWebViewRenderer? 如何在Xamarin Studio中修复“执行任务CreateAdditionalLibraryResourceCache错误” - How do I fix “Error executing task CreateAdditionalLibraryResourceCache” in Xamarin Studio 如何使用Monogame和Xamarin解决号码溢出问题? - How do i fix a Number Overflow with Monogame and Xamarin? Xamarin iOS WkWebView 和 WebRTC 麦克风在后台不起作用 - Xamarin iOS WkWebView and WebRTC microphone not work in background 如何在c#Xamarin中实现ios委托/数据源模式? - How to implement the ios delegate / data source pattern in c# Xamarin? Xamarin iOS-绑定库时如何包括依赖项? - Xamarin iOS - How do I include dependencies when binding libraries? 如何在Xamarin Designer中将图像按钮添加到iOS故事板上? - How do I add an image button to an iOS storyboard in Xamarin Designer? 如何在Xamarin Forms for iOS中创建导航栏? - How do I create a Navigation Bar in Xamarin Forms for iOS? 如何将我的 NSArray 投射到 List<String> 在 xamarin ios 中 - How do I cast my NSArray to List<String> in xamarin ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM