西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

首頁編程開發(fā)其它知識(shí) → WP7 瀏覽器控件WebBrowser歷史記錄、前進(jìn)、后退、刷新功能實(shí)現(xiàn)

WP7 瀏覽器控件WebBrowser歷史記錄、前進(jìn)、后退、刷新功能實(shí)現(xiàn)

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來源:西西整理時(shí)間:2013/4/8 18:11:09字體大。A-A+

作者:西西點(diǎn)擊:0次評(píng)論:0次標(biāo)簽: WebBrowser

瀏覽器密碼查看工具(WebBrowserPassView)V1.70英文綠色免費(fèi)版
  • 類型:密碼相關(guān)大。229KB語言:英文 評(píng)分:5.0
  • 標(biāo)簽:
立即下載

由于要在應(yīng)用程序內(nèi)部訪問網(wǎng)頁,不跳出應(yīng)用。所以要實(shí)現(xiàn)一個(gè)瀏覽器。但是悲催的事windows phone8 的WebBrowser控件已經(jīng)支持了像CanGoBack ,CanGoForward,GoBack,GoForward等這些功能,但是wp7沒有就實(shí)現(xiàn)了幾個(gè)前進(jìn)后退幾個(gè)功能。用在page里面也很簡(jiǎn)單,實(shí)現(xiàn)的效果如下圖所示。

因?yàn)槭褂檬趾?jiǎn)單。和windows phone8上的WebBrowser一樣的。我就直接給控件的代碼,就帖使用的代碼了。

代碼如下,注釋也都寫的比較清楚。

using System;

using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Markup;
using System.Text.RegularExpressions;
using Microsoft.Phone.Tasks;
using System.Collections.Generic;
using System.Windows.Navigation;

namespace Controls.HcControl
{
    public class HcWebView : Control
    {
        /// <summary>
        /// Gets the ControlTemplate string for the control.
        /// </summary>
        /// <remarks>
        /// Not in generic.xaml so the implementation of HcWebView can be entirely in this file.
        /// </remarks>
        private static string Templatestring
        {
            get
            {
                return
                    "<ControlTemplate " +
                        "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
                        "xmlns:phone=\"clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone\" " +
                        "xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
                        "<Grid>" +
                            "<phone:WebBrowser x:Name=\"WebBrowser\" IsScriptEnabled=\"True\"/>" +
                        "</Grid>" +
                    "</ControlTemplate>";
            }
        }

        //----------對(duì)象的內(nèi)部成員
        #region Member

        /// <summary>
        /// WebBrowser.
        /// </summary>
        private WebBrowser _webBrowser;

        /// <summary>
        /// 歷史Uri記錄堆棧
        /// </summary>
        private List<Uri> _historyStack;

        /// <summary>
        /// 歷史記錄堆棧索引
        /// </summary>
        private int _historyStackIndex;

        /// <summary>
        /// 導(dǎo)航到的Uri是否來自歷史堆棧
        /// </summary>
        private bool _fromHistory;

        private bool canGoBack;
        /// <summary>
        /// 獲取一個(gè)值,該值指示 WebBrowser 是否可以在瀏覽歷史記錄中向前導(dǎo)航一個(gè)頁面。
        /// </summary>
        public bool CanGoBack
        {
            get
            {
                return (_historyStackIndex > 1);
            }
            internal set
            {
                canGoBack = value;
            }
        }

        private bool canGoForward;
        /// <summary>
        /// 獲取一個(gè)值,該值指示 WebBrowser 是否可以在瀏覽歷史記錄中向前導(dǎo)航一個(gè)頁面。
        /// </summary>
        public bool CanGoForward 
        {
            get
            {
                return (_historyStackIndex < _historyStack.Count);
            }
            internal set
            {
                canGoForward = value;
            }
        }

        #endregion
        
        //----------對(duì)象的生命周期
        #region LifeCycle

        /// <summary>
        /// Initializes a new instance of the HcWebView class.
        /// </summary>
        public HcWebView()
        {
            this._historyStack = new List<Uri>();
            this._historyStackIndex = 0;
            this._fromHistory = false;

            this.Template = (ControlTemplate)XamlReader.Load(TemplateString);
        }
        
        /// <summary>
        /// Invoked when a new Template is applied.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (_webBrowser != null)
            {
                _webBrowser.LoadCompleted -= OnLoadCompleted;
                _webBrowser.Navigated -= OnNavigated;
                _webBrowser.Navigating -= OnNavigating;
                _webBrowser.NavigationFailed -= OnNavigationFailed;
                _webBrowser.ScriptNotify -= OnScriptNotify;
            }


            _webBrowser = GetTemplateChild("WebBrowser") as WebBrowser;


            if (_webBrowser != null)
            {
                _webBrowser.LoadCompleted += OnLoadCompleted;
                _webBrowser.Navigated += OnNavigated;
                _webBrowser.Navigating += OnNavigating;
                _webBrowser.NavigationFailed += OnNavigationFailed;
                _webBrowser.ScriptNotify += OnScriptNotify;
            }
        }

        #endregion

        //----------對(duì)象響應(yīng)的事件
        #region Events

        /// <summary>
        /// 在 WebBrowser 控件已加載內(nèi)容之后引發(fā)的事件。
        /// </summary>
        public event LoadCompletedEventHandler LoadCompleted;
        private void OnLoadCompleted(object sender, NavigationEventArgs e)
        {
            if (LoadCompleted != null)
            {
                LoadCompleted(this, e);
            }
        }

        /// <summary>
        /// 在 WebBrowser 控件成功導(dǎo)航之后引發(fā)的事件。
        /// <summary>
        public event EventHandler<NavigationEventArgs> Navigated;
        private void OnNavigated(object sender, NavigationEventArgs e)
        {
            if (!_fromHistory)
            {
                if (_historyStackIndex < _historyStack.Count)
                {
                    _historyStack.RemoveRange(_historyStackIndex, _historyStack.Count - _historyStackIndex);
                }
                _historyStack.Add(e.Uri);
                _historyStackIndex += 1;
            }
            _fromHistory = false;
            if (Navigated != null)
            {
                Navigated(this, e);
            }
        }

        /// <summary>
        /// 當(dāng)瀏覽器控件正在導(dǎo)航(包括從重定向)時(shí)引發(fā)的事件。
        /// </summary>
        public event EventHandler<NavigatingEventArgs> Navigating;
        private void OnNavigating(object sender, NavigatingEventArgs e)
        {
            if (Navigating != null)
            {
                Navigating(this, e);
            }
        }

        /// <summary>
        /// 在 WebBrowser 控件導(dǎo)航失敗之后引發(fā)的事件。
        /// </summary>
        public event NavigationFailedEventHandler NavigationFailed;
        private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (NavigationFailed != null)
            {
                NavigationFailed(this, e);
            }
        }

        /// <summary>
        /// 當(dāng) Javascript 調(diào)用 window.external.notify(<data>) 時(shí)引發(fā)的事件
        /// </summary>
        public event EventHandler<NotifyEventArgs> ScriptNotify;
        private void OnScriptNotify(object sender, NotifyEventArgs e)
        {
            if (ScriptNotify != null)
            {
                ScriptNotify(this, e);
            }
        }

        #endregion

        //----------對(duì)象的內(nèi)部函數(shù)
        #region Methods

        /// <summary>
        /// WebBrowser 在瀏覽歷史記錄中向后導(dǎo)航一個(gè)頁面。
        /// </summary>
        public void GoBack()
        {
            if (_historyStackIndex > 1)
            {
                _historyStackIndex -= 1;
                _fromHistory = true;
                _webBrowser.Navigate(_historyStack[_historyStackIndex - 1]);
            }
        }

        /// <summary>
        ///  WebBrowser 在瀏覽歷史記錄中向前導(dǎo)航一個(gè)頁面。
        /// </summary>
        public void GoForward()
        {
            if (_historyStackIndex < _historyStack.Count)
            {
                _historyStackIndex += 1;
                _fromHistory = true;
                _webBrowser.Navigate(_historyStack[_historyStackIndex - 1]);
            }
        }

        /// <summary>
        /// Refresh HcWebView
        /// </summary>
        public void RefreshWebView()
        {
            if ((this._webBrowser) != null && (this._webBrowser.Source != null))
            {
                this._webBrowser.Source = new Uri(this._webBrowser.Source.ToString());
            }
        }

        /// <summary>
        /// 應(yīng)用程序啟動(dòng)“Web 瀏覽器”應(yīng)用程序。
        /// </summary>
        public void ShowWebBrowser()
        {
            WebBrowserTask webTask = new WebBrowserTask();
            if ( (this._webBrowser) != null && (this._webBrowser.Source != null))
            {
                webTask.Uri = this._webBrowser.Source;
            }
            webTask.Show();
        }

        #endregion

        //----------對(duì)象的依賴屬性
        #region DependencyProperty

        #region Source DependencyProperty

        /// <summary>
        /// Gets or sets the Source.
        /// </summary>
        public Uri Source
        {
            get { return (Uri)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        /// <summary>
        /// Identifies the Source dependency property.
        /// </summary>
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Title", typeof(Uri), typeof(HcWebView), new PropertyMetadata(null, new PropertyChangedCallback(OnSourceChanged)));

        /// <summary>
        /// Prevents the webWiew Source from transitioning into a Semiexpanded or Collapsed visual state if the Source is not set.
        /// </summary>
        /// <param name="obj">The dependency object.</param>
        /// <param name="e">The event information.</param>
        private static void OnSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            HcWebView webView = (HcWebView)obj;

            if (e.NewValue != null)
            {
                webView._webBrowser.Source = e.NewValue as Uri;
            }
        }

        #endregion

        #endregion
    }
}

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過審核才能顯示)
    推薦文章

    沒有數(shù)據(jù)