简体   繁体   中英

How to Programmatically Scroll UWP WebView?

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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