如何在UWP中的WebView上获取scrollYOffset?

时间:2017-08-27 12:20:46

标签: webview uwp scrolltop

我在Visual Studio上尝试了这段代码。但它变成了:

  

System.Exception:'来自HRESULT的异常:0x80020101'

我该如何解决?

string function = @"window.external.notify(document.body.scrollTop)";

await PinView.InvokeScriptAsync("eval", new string[] { function });

1 个答案:

答案 0 :(得分:1)

  

System.Exception:'来自HRESULT的异常:0x80020101'

网络视图内容中的脚本可以使用带有 string 参数的window.external.notify将信息发送回您的应用。但是,document.body.scrollTop的类型为number。因此,您应该将参数转换为字符串。

string function = @"window.external.notify(document.body.scrollTop.toString())";
await MyWebView.InvokeScriptAsync("eval", new string[] { function});

要接收这些消息,请处理ScriptNotify事件。

private void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
{
    MyText.Text = e.Value.ToString();
}