【发布时间】:2018-08-21 10:27:42
【问题描述】:
因为在知道人们一直在做什么之前只是调用实际网页中已经存在的脚本。
private void LoadHtml()
{
WebViewControl.Source = new Uri("https://www.twitter.com");
}
首先我加载一个我想修改其样式的页面。我可以通过注入应用程序本地文件夹中可用的 CSS 文件来做到这一点。
private async void InvokeScript()
{
// Invoke the javascript function called 'addCSS' that is loaded into the WebView.
try
{
string result = await WebViewControl.InvokeScriptAsync("addCSS", null);
rootPage.NotifyUser($"Script returned \"{result}\"", NotifyType.StatusMessage);
}
catch (Exception ex)
{
string errorText = null;
switch (ex.HResult)
{
case unchecked((int)0x80020006):
errorText = "There is no function called doSomething";
break;
case unchecked((int)0x80020101):
errorText = "A JavaScript error or exception occured while executing the function doSomething";
break;
case unchecked((int)0x800a138a):
errorText = "doSomething is not a function";
break;
default:
// Some other error occurred.
errorText = ex.Message;
break;
}
rootPage.NotifyUser(errorText, NotifyType.ErrorMessage);
}
然后我会调用 addCSS 函数。当然这不会起作用,因为 Twitter.com JS Code 没有这个功能。
我想知道的是如何调用一个 JS 函数,它将一个新的 CSS 文件放入 webview 中的当前网页中。
【问题讨论】: