How to Programmatically Scroll UWP WebView?

青春壹個敷衍的年華 提交于 2020-01-06 02:21:30

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/36500747/how-to-programmatically-scroll-uwp-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!