David David - 1 year ago 388
CSS Question

UWP Visual C# WebView InvokeScript to change CSS throws Exception

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:


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


private void webViewCSS_LoadCompleted (object sender, NavigationEventArgs e)

private void addCss()
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

Answer Source

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 JavaScript eval function to inject the content into the web page.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download