[英]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.