【问题标题】:Xamarin.forms webview auto height with contentXamarin.forms webview 自动高度与内容
【发布时间】:2018-01-19 06:53:12
【问题描述】:

我在我的页面上使用 Webview 和 Label 控件。一旦 WebView 滚动到其内容的末尾,我想显示标签。 这是一张显示正在发生的事情的图片:

标签始终位于底部。

我试过这段代码:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <WebView Grid.Row="0"  Source="{Binding DetailHtml}"></WebView>
    <Label Grid.Row="1" Text="TIN LIÊN QUAN" TextColor="#5ea201" FontAttributes="Bold"/>
</Grid>

还有这个:

<StackLayout>
    <WebView Source="{Binding DetailHtml}"></WebView>
    <Label  Text="TIN LIÊN QUAN" TextColor="#5ea201" FontAttributes="Bold"/>
</StackLayout>

我也尝试使用 WebView 的自定义渲染器来设置高度,但它不起作用:

[assembly: ExportRenderer(typeof(WebView), typeof(WebViewCustomRenderer))]
namespace SucKhoeGiaDinh.iOS.Renderers
{
    public class WebViewCustomRenderer : WebViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
            Delegate = new ExtendedUIWebViewDelegate(this);
        }
    }

    public class ExtendedUIWebViewDelegate : UIWebViewDelegate
    {
        WebViewCustomRenderer webViewRenderer;

        public ExtendedUIWebViewDelegate(WebViewCustomRenderer _webViewRenderer = null)
        {
            webViewRenderer = _webViewRenderer ?? new WebViewCustomRenderer();
        }

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

我可以使用另一种方法来实现这一点吗?

【问题讨论】:

  • 尝试将两者都添加到 ListViewTemplateSelector 中。但是,这是一个棘手的问题,因为您将在可滚动的 ListView 内有一个可滚动的元素 (WebView)。
  • @DennisSchröer 我尝试将两者都添加到 ListView 中。它不工作,webview 是空的
  • 第一个(Grid)有什么问题?
  • @ColeXia 与 Grid ,标签始终固定在底部

标签: xamarin xamarin.forms xamarin.ios xamarin.android


【解决方案1】:

我解决了我的问题。

<ScrollView >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>
        <WebView Grid.Row="0"  Source="{Binding DetailHtml}" />
        <Label Grid.Row="1" Text="TIN LIÊN QUAN" TextColor="#5ea201" FontAttributes="Bold"/>
    </Grid>
</ScrollView>

并自定义渲染 webview 以按内容设置高度。

【讨论】:

  • 自定义渲染中按内容设置的高度是什么样的?
  • 这个问题的答案在您忘记粘贴的自定义渲染器中。
猜你喜欢
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
  • 2014-09-29
  • 2021-05-24
  • 2021-08-28
  • 1970-01-01
  • 2016-11-29
  • 2013-08-20
相关资源
最近更新 更多