I am trying to change the CSS of a loaded Website in a WebView (not local) by injecting a Javascript Function. I found these two Articles ( Article 1 , Article 2 ) but after implementing both Versions I always get the following Exception: System.NotImplementedException {"The method or operation is not implemented."}
This is my Code so far:
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)
{
}
}
...
I dont know why it is throwing an Exception. I hope the Information that I have given is enough. If not please get back to me :-)
Thank you in advance for your answer
WebView.InvokeScript method may be unavailable for releases after Windows 8.1. Instead, we need to use WebView.InvokeScriptAsync method .
You can refer to WebView class , there are examples in the Remarks part of the official document:
you can use
InvokeScriptAsync
with the JavaScripteval
function to inject the content into the web page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.