我正在尝试通过注入 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)
{
}
}
...
我不知道它为什么会抛出异常。我希望我提供的信息已经足够了。如果没有,请回复我:-)
预先感谢您的回答