是否有脚本通知不适用于UWP ms-appdata的解决方法
我正在开发一个应用程序Xamarin.UWP
,它试图将Javascript注入到本地html文件(uri:ms-appdata:/// local/index。 HTML)像这样:是否有脚本通知不适用于UWP ms-appdata的解决方法
async void OnWebViewNavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
if (args.IsSuccess)
{
// Inject JS script
if (Control != null && Element != null)
{
foreach (var f in Element.RegisteredFunctions.Where(ff => !ff.IsInjected))
{
await Control.InvokeScriptAsync("eval", new[] { string.Format(JavaScriptFunctionTemplate, f.Name) });
f.Injected();
}
}
}
}
然后,当Javascript方法被调用,这将调用OnWebViewScriptNotify
方法,这样我可以proccess在我的应用程序的请求。
麻烦的是一些kind of security reasons这并不工作:
这是我们做了,我们已经有反馈,所以我们 重新评估它的政策决定。如果您将 NavigateToStreamUri与解析器对象一起使用,则相同的限制不适用。在内部, ms-appdata:///会发生什么情况。
然后我试了一下在这种情况下,这是使用一个解析器这里提到建议:https://stackoverflow.com/a/18979635/2987066
但是这会对性能产生巨大的影响,因为它是所有的文件不断地转换成流加载,以及某些页面加载不正确。
我又看了看使用AddWebAllowedObject
方法,像这样:
private void Control_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
if (Control != null && Element != null)
{
foreach (var f in Element.RegisteredFunctions)
{
var communicator = new HtmlCommunicator(f);
Control.AddWebAllowedObject("HtmlCommunicator", communicator);
}
}
}
其中HtmlCommunicator
是:
[AllowForWeb]
public sealed class HtmlCommunicator
{
public JSFunctionInjection Function { get; set; }
public HtmlCommunicator(JSFunctionInjection function)
{
Function = function;
}
public void Fred()
{
var d = 2;
//Do something with Function
}
}
,并在我的HTML它就像这样:
try { window.HtmlCommunicator.Fred(); } catch (err) { }
但这也不起作用。
那么有没有办法解决这个荒谬的限制?
所以我发现了这个答案:C# class attributes not accessible in Javascript
它说:
我相信你需要定义开始以较低的 字母字符的方法名。
例如:将GetIPAddress更改为getIPAddress。
我在我身边进行了测试,发现如果我使用大写字母名称 'GetIPAddress',它将不起作用。但是,如果我使用getIPAddress,它会起作用。
所以,我想这一点:
我创建Windows Runtime Component
类型的新项目的建议here,我改变了我的方法名称为小写,所以我不得不:
[AllowForWeb]
public sealed class HtmlCommunicator
{
public HtmlCommunicator()
{
}
public void fred()
{
var d = 2;
//Do something with Function
}
}
在我的javascript我然后得到:
try { window.HtmlCommunicator.fred(); } catch (err) { }
和在我的主要UWP项目我引用了新的Windows Runtime Component
图书馆,并有以下内容:
public HtmlCommunicator communicator { get; set; }
private void Control_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
if (Control != null && Element != null)
{
communicator = new HtmlCommunicator();
Control.AddWebAllowedObject("HtmlCommunicator", communicator);
}
}
这工作!
你可以提供一个小函数,它返回来自HtmlCommunicator类的模型,而不管其为空。 –
从[http://blog.damiendelaire.com/2016/06/communicating-back-and-forth-with.html](http://blog.damiendelaire.com/2016/06/communicating-back- and-forth-with.html)我读到'因为一些奇怪的原因,你需要添加在新的WRC库中创建这个标记,并将这个类标记为[AllowForWeb]'我将调查更多 – user1
我在上面的链接中注意到[此链接](https://www.suchan.cz/2016/01/hacking-uwp-webview-part-2-bypassing-window-external-notify-whitelist/)他们都使用'ms-app-web',所以我'我不确定最后一种方法是否适用于'ms-app-data'。将该课程放在另一个WRC库中不起作用 – user1