c# - Windows 8.1 ScriptNotify 不工作

标签 c# javascript xaml visual-studio-2013 windows-store-apps

我对 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');

另见 descriptionsample在 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/

相关文章:

c# - 如何使用 C# System.Diagnostics.Process 与提示交互?

c# - 使用 ODBC 从列名中包含空格的 CSV 文件中进行选择

c# - 无法在 Razor 组件中呈现原始 html

javascript - J2V8是使用android系统V8还是自己编译提供的?

javascript - 更好的 JS if 条件编程风格

c# - WPF 用户控件附加到边框时显得模糊

c# - 如何在 WPF 中实现使用祖先方法的命令?

wpf - 调整 XAML Grid.Background 图像的大小

c# - 最小化应用程序表单会导致模态表单关闭,但不会关闭 MessageBox

javascript - Sinon stub 未正确替换 stub 函数