【问题标题】:Xamarin Forms WebView: Scroll to bottom of HTML DocumentXamarin Forms WebView:滚动到 HTML 文档的底部
【发布时间】:2021-10-10 17:15:30
【问题描述】:

如何在 WebView 中滚动到底部?我已经尝试过了,但它没有用:

webView.EvaluateJavaScriptAsync("window.scrollTo(0,document.body.scrollHeight);");

【问题讨论】:

    标签: c# android ios xamarin uwp


    【解决方案1】:

    您可以在按钮点击事件中使用下面的代码。

       webview.Eval("window.scrollTo(0, document.body.scrollHeight);");
    

    或者你可以通过自定义渲染器来做到这一点。 html 文档显示时会滚动到底部。

    安卓:

    [assembly: Xamarin.Forms.ExportRenderer(typeof(ScrollWebView), typeof(ScrollWebViewRenderer))]
    namespace App8.Droid
    {
    public class ScrollWebViewRenderer : WebViewRenderer
    {
        public static ScrollWebView view = null;
    
        public ScrollWebViewRenderer(Context context) : base(context)
        {
        }
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
    
            Control.SetWebViewClient(new webviewClient(this, "window.scrollTo(0,document.body.scrollHeight);"));
        }
    }
    public class webviewClient : FormsWebViewClient
    {
        string _javascript;
        public webviewClient(ScrollWebViewRenderer renderer, string javascript) : base(renderer)
        {
            _javascript = javascript;
        }
        public override void OnPageFinished(Android.Webkit.WebView view, string url)
        {
            base.OnPageFinished(view, url);
            view.EvaluateJavascript(_javascript, null);
        }
    }
    }
    

    【讨论】:

    • 不幸的是,它不起作用:(但感谢您的关注
    • 你能提供更多细节让我重现吗?我使用 webview 加载 html 字符串,当我将代码放入按钮单击事件时,它可以滚动到底部。
    • @hyperpixel34 您可以查看我的更新以在自定义渲染器中执行此操作。
    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    相关资源
    最近更新 更多