【问题标题】:Get the content height from a Webview in Xamarin Forms UWP从 Xamarin Forms UWP 中的 Webview 获取内容高度
【发布时间】:2017-02-23 02:22:46
【问题描述】:

我正在尝试使用自定义渲染器获取在 Web 视图中渲染的网页的高度。我已经成功连接到 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
            }

        }
    }

【问题讨论】:

标签: c# xamarin xamarin.forms uwp uwp-xaml


【解决方案1】:

这是我降落的地方。对 UWP 知之甚少,因此请随时提出改进建议,但这似乎运作良好。请注意,在我的情况下,我从自定义控件传递 HTML 字符串以呈现/“导航”到,如果您要访问实际页面,只需使用 Control.Navigate(Uri source) .

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;
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2017-10-18
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    相关资源
    最近更新 更多