如何以编程方式滚动 UWP WebView? [英] How to Programmatically Scroll UWP WebView?

查看:29
本文介绍了如何以编程方式滚动 UWP WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 webview 制作一个简单的 UWP(通用 Windows 10 应用程序)网络应用程序.如何单击按钮返回页面顶部?我在 MSDN 上找不到.

I'm making a simply UWP (Universal Windows 10 App) web app using a webview. How can I return on the top of the page clicking on a button? I can't find it on the MSDN.

推荐答案

您可以使用 window.scrollTo(0,0) 方法,它是一种将文档滚动到0"位置的 JavaScript 方法" 水平方向和 "0" 垂直方向.

You can use window.scrollTo(0,0) method, it is a JavaScript method that scrolls the document to position "0" horizontally and "0" vertically.

您可以与的内容进行交互WebView 通过使用 InvokeScriptAsync 调用或注入脚本的方法WebView 内容.

You can interact with the content of the WebView by using the InvokeScriptAsync method to invoke or inject script into the WebView content.

例如:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <WebView x:Name="theWebView" Source="https://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.webview.aspx">
    </WebView>
    <Button Content="To The Top" Click="Button_Click"></Button>
</Grid>

背后的代码:

private string ScrollToTopString = @"window.scrollTo(0,0);";

private async void Button_Click(object sender, RoutedEventArgs e)
{
    await theWebView.InvokeScriptAsync("eval", new string[] { ScrollToTopString });
}

这篇关于如何以编程方式滚动 UWP WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆