gpt4 book ai didi

c# - 如何使用 CustomWebViewRenderer 使 WKWebView 的高度适合其在 Xamarin 中的内容?

转载 作者:行者123 更新时间:2023-12-01 16:01:54 24 4
gpt4 key购买 nike

我为 iOS 制作了一个自定义 webview 渲染器,我在其中手动将自定义 WebView 替换为 WKWebView。但是,我的主要目的是让 WKWebView 根据它的内容调整它的高度。它的内容由带有正文的预定义 HTML 字符串组成。

现在它切断了 Webview,其余的内容被塞进了一个 ScrollView 。我在 webview 之前放置的图像和文本也固定在原地,同时我希望它们也向下滚动。我不想要 webview 的滚动条,我只希望 webview 的大小与其内容相符,以便用户可以向上滑动查看整个文本。

CustomWebViewRenderer.cs

[assembly: ExportRenderer(typeof(PostWebView), typeof(Yoors.iOS.CustomWebViewRenderer))]
namespace Yoors.iOS
{
public class CustomWebViewRenderer : ViewRenderer<PostWebView, WKWebView>
{
WKWebView _wkWebView;
protected override void OnElementChanged(ElementChangedEventArgs<PostWebView> e)
{
base.OnElementChanged(e);

if (Control == null)
{
WKWebViewConfiguration config = new WKWebViewConfiguration();

_wkWebView = new WKWebView(Frame, config);


SetNativeControl(_wkWebView);
}
if (e.NewElement != null)
{

HtmlWebViewSource source = (Xamarin.Forms.HtmlWebViewSource)Element.Source;
string headerString = "<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'></header>";
string html= headerString + source.Html;
Console.WriteLine("Height" + Element);
_wkWebView.LoadHtmlString(html, baseUrl: null);
_wkWebView.ScrollView.ScrollEnabled = false;
_wkWebView.SizeToFit();
}




}
}

PostWebView.xaml
<?xml version="1.0" encoding="UTF-8"?>
<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>

PostView.xaml
 <ContentPage.Content>
<ScrollView>
<StackLayout BackgroundColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<!-- Header of the post-->
<Image HeightRequest="125" Aspect="AspectFill" Source="{Binding Post.ImageUrl}" />
<StackLayout BackgroundColor="White" Padding="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Margin="10, 0,10, 0">
<Label Text="{Binding Post.Title}" FontFamily="{StaticResource BoldFont}" FontSize="20" />
<Label Text="{Binding Post.CreatedOn.DisplayText}" FontSize="12" />
<!-- Content of the post in a HTML view-->
</StackLayout>
<templates:PostWebView VerticalOptions="FillAndExpand" BindingContext="{Binding Post}">
</templates:PostWebView>
<templates:CommentView BindingContext="{Binding CommentsViewModel}">
<x:Arguments>
<ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
</x:Arguments>
</templates:CommentView>
</StackLayout>
</ScrollView>
</ContentPage.Content>

我认为为 PostWebView 放置 VerticalOptions= "FillAndExpand"会使其适合其内容的大小,而无需创建 scollbar,因此不会将其他内容固定到位,但它只是这样做: [![this][1]][1].有谁知道如何提供帮助?

最佳答案

我通过制作一个在 Webview 完成加载时注册的 CustomNavigationDelegate 回答了我自己的问题。

CustomNavigationDelegate.cs

public class CustomWKNavigationDelegate : WKNavigationDelegate
{

CustomWebViewRenderer _webViewRenderer;

public CustomWKNavigationDelegate(CustomWebViewRenderer webViewRenderer)
{
_webViewRenderer = webViewRenderer;
}

public override async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
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;
}
}
}

然后在上面显示的 CustomWebViewRenderer 的 OnElementChanged 中,我将 Navigation Delegate 分配给 WKWebView。

CustomWebViewRenderer.cs
...
protected override void OnElementChanged(ElementChangedEventArgs<PostWebView> e)
{
base.OnElementChanged(e);


if (Control == null)
{
WKWebViewConfiguration config = new WKWebViewConfiguration();

_wkWebView = new WKWebView(Frame, config);
_wkWebView.NavigationDelegate = new CustomNavigationDelegate(this);
...

这将使 WKWebView 具有其内容的大小!

关于c# - 如何使用 CustomWebViewRenderer 使 WKWebView 的高度适合其在 Xamarin 中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56303696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com