为了账号安全,请及时绑定邮箱和手机立即绑定

自动调整WebView的大小以适合内容

自动调整WebView的大小以适合内容

C#
Helenr 2021-04-26 17:13:22
我正在研究Xamarin.Forms PCL项目,该项目以WebView的形式显示帖子,因此我添加了可点击的内容(例如#标签)。我遇到的问题是WebView无法适应其内容的大小。WebView不会加载实际的网站,我正在使用以下方法将HTML绑定到ListView中的每个帖子"<html><p>" + body + "</p></html>"我尝试研究自定义渲染器,并按照本教程进行操作,最后得到以下代码对于Android,我使用了#pragma warning disable CS0618 // Type or member is obsoletepublic class CustomWebViewAndroid : WebViewRenderer{    static CustomWebView _xwebView = null;    WebView _webView;                class ExtendedWebViewClient : Android.Webkit.WebViewClient    {        public override async void OnPageFinished (WebView view, string url)        {            if (_xwebView != null) {                int i = 10;                while (view.ContentHeight == 0 && i-- > 0) // wait here till content is rendered                    await System.Threading.Tasks.Task.Delay (100);                _xwebView.HeightRequest = view.ContentHeight;            }            base.OnPageFinished (view, url);        }    }    protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.WebView> e)    {        base.OnElementChanged (e);         _xwebView = e.NewElement as CustomWebView;        _webView = Control;        if (e.OldElement == null) {                            _webView.SetWebViewClient (new ExtendedWebViewClient ());        }             }       }对于iOSpublic class CustomWebViewiOS : WebViewRenderer{    protected override void OnElementChanged(VisualElementChangedEventArgs e)    {        base.OnElementChanged(e);        Delegate = new CustomWebViewDelegate(this);    }}public class CustomWebViewDelegate : UIWebViewDelegate{    CustomWebViewiOS webViewRenderer;    public CustomWebViewDelegate(CustomWebViewiOS _webViewRenderer = null)    {        webViewRenderer = _webViewRenderer ?? new CustomWebViewiOS();    }在iOS上,它无法正确隔开帖子,并切断了其上方的时间戳。我的猜测是ViewCell的高度会根据内容进行调整,但不会考虑其他帖子。同样对于iOS,如果文本需要两行,则需要滚动查看其余内容。
查看完整描述

2 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超6个赞

要显示HTML内容,您可以使用它,它对我来说就像是魅力,我们不需要使用渲染器来适应内容的大小。

<Label BackgroundColor="White" Text="{Binding description}" TextType="Html"/>


查看完整回答
反对 回复 2021-05-08
  • 2 回答
  • 0 关注
  • 212 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信