简体   繁体   English

如何防止WP7中的WebBrowser控件中的AJAX导航离开页面

[英]How to prevent AJAX navigation away from page in WebBrowser control in WP7

I have a page loaded onto my webBrowser control, and I want to prevent navigation away to a certain URL, based on some condition. 我有一个页面加载到我的webBrowser控件上,并且我想根据某些情况阻止导航到某个URL。 However all these navigations away are asynchronous, through AJAX. 但是,所有这些导航都是通过AJAX异步进行的。 I have tried using the e.Cancel = true; 我已经尝试使用e.Cancel = true; to prevent navigation away, but it still navigates away. 以防止导航,但它仍然可以导航。 What can I do to prevent this navigation? 我该怎么做才能防止这种导航?

    private void webBrowser_Navigating(object sender, Microsoft.Phone.Controls.NavigatingEventArgs e)
    {
        MessageBox.Show("Navigating"); //This gets executed during AJAX navigation, which means the Navigating event occurs even during AJAX navigations
        if (e.Uri.ToString().Contains("#!/restricted"))
        {
            MessageBox.Show("URL Found. Trying to cancel"); //This also gets executed when navigating away to that specific link. AJAX link, like ../index.php#!/restricted.php
            e.Cancel = true;
            return;
        }
     }

Printing the webBrowser.Source.ToString() in a MessageBox after execution still shows the first URL as the webBrowser's source, as though it hasn't navigated at all, though it still navigates away. 执行后在MessageBox中打印webBrowser.Source.ToString()仍然显示第一个URL作为webBrowser的源,好像它根本没有导航,尽管它仍然可以导航。 Disabling JavaScript isn't an option, as some other elements of the main page still require JavaScript. 不能选择禁用JavaScript,因为主页的其他某些元素仍然需要JavaScript。

Thanks. 谢谢。

您可以尝试编辑页面的JS代码,并从其中删除所有ajax导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM