0

我正在尝试通过注入 Javascript 函数来更改 WebView(非本地)中加载网站的 CSS。我找到了这两篇文章(第 1条,第 2 条),但是在实现了这两个版本之后,我总是得到以下异常: System.NotImplementedException {“方法或操作未实现。”}

到目前为止,这是我的代码:

主页.xml

...
  <WebView x:Name="WebView" LoadCompleted="webViewCSS_LoadCompleted"/>
...

MainPage.xaml.cs

...
private void webViewCSS_LoadCompleted (object sender, NavigationEventArgs e)
  {
     addCss();
  }

private void addCss()
  {
     try
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("function setCSS() { ");
            sb.Append("document.getElementsByClassName('app')[0].style.width = '100%';");
            sb.Append("} ");

            string script = sb.ToString();
            WebView.InvokeScript("eval", new string[] { script });
            WebView.InvokeScript("eval", new string[] { "setCSS()" });
        } catch (Exception ex)
        {

        }
    }
...

我不知道它为什么会抛出异常。我希望我提供的信息已经足够了。如果没有,请回复我:-)

预先感谢您的回答

4

1 回答 1

2

WebView.InvokeScript 方法可能不适用于 Windows 8.1 之后的版本。相反,我们需要使用WebView.InvokeScriptAsync 方法

可以参考WebView类,官方文档Remarks部分有例子:

您可以使用InvokeScriptAsyncJavaScripteval函数将内容注入网页。

于 2016-07-04T04:50:16.480 回答