【问题标题】:How to correctly handle web browser control back navigation如何正确处理 Web 浏览器控制返回导航
【发布时间】:2012-09-17 19:47:47
【问题描述】:

我正在尝试在我放置在我的 WP7 应用程序中的 Web 浏览器控件中执行正确的返回堆栈导航。 Web 浏览器位于一个自定义控件中,我在其中实现了导航方法,然后将此控件放置在我的应用程序的一个页面中。导航似乎工作正常,除了 backstack。有时它会返回上一页,而其他时候(当它不应该这样做时)它会关闭应用程序。到目前为止,这是我的实现:

WebBrowser.cs(用户控制)

    //The navigation urls of the browser.
    private readonly Stack<Uri> _NavigatingUrls = new Stack<Uri>();

    //The history for the browser
    private readonly ObservableCollection<string> _History =
       new ObservableCollection<string>();

    //Flag to check if the browser is navigating back.
    bool _IsNavigatingBackward = false;


    /// <summary>
    /// Gets the History property for the browser.
    /// </summary>
    public ObservableCollection<string> History
    {
        get { return _History; }
    }

    /// <summary>
    /// CanNavigateBack Dependency Property
    /// </summary>
    public static readonly DependencyProperty CanNavigateBackProperty =
        DependencyProperty.Register("CanNavigateBack", typeof(bool),
        typeof(FullWebBrowser), new PropertyMetadata((bool)false));

    /// <summary>
    /// Gets or sets the CanNavigateBack property. This dependency property
    /// indicates whether the browser can go back.
    /// </summary>
    public bool CanNavigateBack
    {
        get { return (bool)GetValue(CanNavigateBackProperty); }
        set { SetValue(CanNavigateBackProperty, value); }
    }


    void TheWebBrowser_Navigating(object sender,
        Microsoft.Phone.Controls.NavigatingEventArgs e)
    {
        //show the progress bar while navigating
    }

    void TheWebBrowser_Navigated(object sender,
        System.Windows.Navigation.NavigationEventArgs e)
    {
        //If we are Navigating Backward and we Can Navigate back,
        //remove the last uri from the stack.
        if (_IsNavigatingBackward == true && CanNavigateBack)
            _NavigatingUrls.Pop();

        //Else we are navigating forward so we need to add the uri
        //to the stack.
        else
        {
            _NavigatingUrls.Push(e.Uri);

            //If we do not have the navigated uri in our history
            //we add it.
            if (!_History.Contains(e.Uri.ToString()))
                _History.Add(e.Uri.ToString());
        }

        //If there is one address left you can't go back.
        if (_NavigatingUrls.Count > 1)
            CanNavigateBack = true;
        else
            CanNavigateBack = false;

        //Finally we hide the progress bar.
        ShowProgress = false;
    }

    /// <summary>
    /// Used to navigate back.
    /// </summary>
    public void NavigateBack()
    {
        _IsNavigatingBackward = true;
        TheWebBrowser.InvokeScript("eval", "history.go(-1)");
    }

BrowserPage.xaml.cs

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        if (TheBrowser.CanNavigateBack)
        {
            e.Cancel = true;
            TheBrowser.NavigateBack();
        }
        else
            base.OnBackKeyPress(e);
    }

注意:我的网络浏览器中启用了 Javascript,并且所有其他导航都正常工作。我的问题是,有时网络浏览器可以正确导航回来,而其他时候它不起作用并一起关闭。我的执行有问题吗?我可以做些什么来解决这个问题?

【问题讨论】:

    标签: c# windows-phone-7 webbrowser-control


    【解决方案1】:

    是我还是你在调用 base.OnBackKeyPress(e) 时创建了一个无限循环引用 - 所以当向后导航不可用时,它会尝试将事件重新发送回自身,事实上,你不需要这样做,不做 e.Cancel = true;您将自动允许 OnBackKeyPress 事件继续进行而不会出现任何问题,因此请完全删除 else 部分,看看会发生什么。由于您在返回导航不可用时收到错误消息,因此只有在那时才启动循环引用是有道理的。

    请记住,我是一名 VB.NET 程序员,所以除非 C# 有一些我没有掌握的非常不同的东西,否则我们可能会找到解决方案。

    事实上你正在做一个 e.cancel=true;建议如果你没有这样做,事件将继续,因此,重新启动事件是没有必要的(因为你没有取消它),并且可能正在创建一个循环引用。尝试删除它,让我们知道会发生什么。也就是说,您需要删除以下行:

     } 
            else 
                base.OnBackKeyPress(e); 
    

    【讨论】:

    • 抱歉,回复延迟太久。我确实尝试删除 else 语句,尽管同样的问题仍然存在。有时导航是正确的,而其他时候应用程序不会从使用 Web 浏览器的页面导航到上一页?
    • @Matthew 您是否意识到浏览器会在您实际按下后退空格按钮时开始后退导航但是,在抬起它之前......所以如果您执行后退按钮按下并按住它下来,它会一直回去,直到没有什么可以回去的。因此在 KeyPress 期间拦截意味着您拦截了组合键按下 + 按下键事件,因此浏览器可能会返回然后运行您的代码,这会导致错误或双返回导航。得到我?尝试在退格键下拦截并将上面的代码放在该事件中,让我知道它是怎么回事。
    猜你喜欢
    • 2021-12-31
    • 2020-02-25
    • 1970-01-01
    • 2019-09-22
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    相关资源
    最近更新 更多