我对 webview 和 scriptnotify 的问题感到困惑
Server chap在网页中放入了以下代码: javascript(警报)和 javascript(确认)
我放入了一个 Debug Writeline 以查看是否正在调用该方法,但实际上没有。
我可以确认 https://webpage.com在 list 中作为接受的 Uri。
这是我的代码
XAML
<Grid>
<WebView x:Name="webView1" HorizontalAlignment="Stretch" ScriptNotify="MyWebView_ScriptNotify" VerticalAlignment="Stretch" />
</Grid>
C#
public TeamPage()
{
this.InitializeComponent();
webView1.ScriptNotify += MyWebView_ScriptNotify;
//other stuff
}
async void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
{
Debug.WriteLine("in here");
var jsScriptValue = e.Value;
MessageDialog msg = new MessageDialog(jsScriptValue);
var res = await msg.ShowAsync();
}
如前所述,甚至没有调用 MyWebView_ScriptNotify??在这个问题上似乎没有太多例子,所以任何帮助将不胜感激。谢谢。
最佳答案
ScriptNotify 不会在 javascript 警报或确认时触发。
如果网页在 JavaScript 中执行以下操作,它将触发:
window.external.notify('some value');
另见 description和 sample在 MSDN 中
编辑:如果您需要收到有关警报的通知并确认您可以这样做:
// do this after initialization of the webview
this.MyWebView.NavigationCompleted += MyWebView_NavigationCompleted;
...
async void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
// this will override the alert function with a function calling
// the window.external.notify function
// you should be able to do the same with confirm
string inject =
@"window.alert = function(arg) {
window.external.notify(arg);
};";
await MyWebView.InvokeScriptAsync("eval", new List<string>() { inject });
}
关于c# - Windows 8.1 ScriptNotify 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23820389/