简体   繁体   English

如何以编程方式滚动UWP WebView?

[英]How to Programmatically Scroll UWP WebView?

I'm making a simply UWP (Universal Windows 10 App) web app using a webview. 我正在使用webview制作一个简单的UWP(通用Windows 10应用程序)Web应用程序。 How can I return on the top of the page clicking on a button? 如何单击按钮返回页面顶部? I can't find it on the MSDN. 我在MSDN上找不到它。

You can use window.scrollTo(0,0) method, it is a JavaScript method that scrolls the document to position "0" horizontally and "0" vertically. 您可以使用window.scrollTo(0,0)方法,这是一个JavaScript方法,可将文档滚动到水平位置“ 0”和垂直位置“ 0”。

You can interact with the content of the WebView by using the InvokeScriptAsync method to invoke or inject script into the WebView content. 您可以使用InvokeScriptAsync方法调用脚本或将脚本插入WebView内容中,从而与WebView的内容进行交互。

For example: 例如:

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

Code behind: 后面的代码:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM