从Xamarin Forms UWP的Webview中获取内容高度

本文关键字:获取 高度 Webview Xamarin Forms UWP | 更新日期: 2023-09-27 18:13:09

我正在尝试使用自定义渲染器在Webview中渲染网页的高度。我已经设法挂钩到Load_Completed事件,正确地触发当页面已经完全渲染,但似乎没有任何暴露内容的高度。

我可以用什么来得到这个?

提前感谢。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Xamarin.Forms.Platform.UWP;
    using Xamarin.Forms;
    using Windows.UI.Xaml.Navigation;
    using Windows.UI.Xaml.Controls;
    [assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(ExtendedViewWebRenderer))] 
    namespace Project.UWP.CustomRenderers
    {
        public class ExtendedViewWebRenderer : WebViewRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                    Control.LoadCompleted += Load_Completed;
                }
            }
            private void Load_Completed(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
            {
                var _webView = (Windows.UI.Xaml.Controls.WebView)sender;
               //Grab content's height here
            }
        }
    }

从Xamarin Forms UWP的Webview中获取内容高度

我在这里着陆了。对UWP了解甚少,所以请随意提出改进建议,但这似乎工作得相当好。请注意,在我的情况下,我从我的自定义控件传递一个HTML字符串来渲染/"导航",如果你要去一个实际的页面,只需使用 control。导航(Uri源)代替

public class ExtendedWebViewRenderer : ViewRenderer<ExtendedWebView, Windows.UI.Xaml.Controls.WebView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ExtendedWebView> e)
        {
            try
            {
                base.OnElementChanged(e);
                if (e.OldElement != null && Control != null)
                {
                    Control.NavigationCompleted -= OnWebViewNavigationCompleted;
                }
                if (e.NewElement != null)
                {
                    if (Control == null)
                    {
                        SetNativeControl(new Windows.UI.Xaml.Controls.WebView());
                    }
                    Control.NavigationCompleted += OnWebViewNavigationCompleted;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error at ExtendedWebViewRenderer OnElementChanged: " + ex.Message);
            }
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
        // update this based on your custom webview control and what you want it to do
            if (Element is ExtendedWebView element && e.PropertyName.Equals(nameof(ExtendedWebView.Html)) && !string.IsNullOrWhiteSpace(element.Html))
                Control.NavigateToString(element.Html); 
        }
        private async void OnWebViewNavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
        {
            if (!args.IsSuccess)
                return;
            var heightString = await Control.InvokeScriptAsync("eval", new[] {"document.body.scrollHeight.toString()" });
            if (int.TryParse(heightString, out int height))
            {
                Element.HeightRequest = height;
            }
            var widthString = await Control.InvokeScriptAsync("eval", new[] {"document.body.scrollWidth.toString()" });
            if (int.TryParse(widthString, out int width))
            {
                Element.WidthRequest = width;
            }
        }
    }