【问题标题】:UWP Visual C# WebView InvokeScript to change CSS throws ExceptionUWP Visual C# WebView InvokeScript 更改 CSS 引发异常
【发布时间】:2016-11-04 23:10:58
【问题描述】:

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

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

MainPage.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)
        {

        }
    }
...

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

提前感谢您的回答

【问题讨论】:

    标签: c# css webview uwp invokescript


    【解决方案1】:

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

    可以参考WebView class,官方文档备注部分有例子:

    您可以使用 InvokeScriptAsync 和 JavaScript eval 函数将内容注入网页。

    【讨论】:

      猜你喜欢
      • 2018-03-14
      • 1970-01-01
      • 2012-05-05
      • 2017-11-04
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多