西西软件下载最安全的下载网站、值得信赖的软件下载站!

首页编程开发其它知识 → WP7 浏览器控件WebBrowser历史记录、前进、后退、刷新功能实现

WP7 浏览器控件WebBrowser历史记录、前进、后退、刷新功能实现

相关软件相关文章发表评论 来源:西西整理时间:2013/4/8 18:11:09字体大小:A-A+

作者:西西点击:0次评论:0次标签: WebBrowser

  • 类型:密码相关大小:229KB语言:英文 评分:5.0
  • 标签:
立即下载

由于要在应用程序内部访问网页,不跳出应用。所以要实现一个浏览器。但是悲催的事windows phone8 的WebBrowser控件已经支持了像CanGoBack ,CanGoForward,GoBack,GoForward等这些功能,但是wp7没有就实现了几个前进后退几个功能。用在page里面也很简单,实现的效果如下图所示。

因为使用十分简单。和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>";
            }
        }

        //----------对象的内部成员
        #region Member

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

        /// <summary>
        /// 历史Uri记录堆栈
        /// </summary>
        private List<Uri> _historyStack;

        /// <summary>
        /// 历史记录堆栈索引
        /// </summary>
        private int _historyStackIndex;

        /// <summary>
        /// 导航到的Uri是否来自历史堆栈
        /// </summary>
        private bool _fromHistory;

        private bool canGoBack;
        /// <summary>
        /// 获取一个值,该值指示 WebBrowser 是否可以在浏览历史记录中向前导航一个页面。
        /// </summary>
        public bool CanGoBack
        {
            get
            {
                return (_historyStackIndex > 1);
            }
            internal set
            {
                canGoBack = value;
            }
        }

        private bool canGoForward;
        /// <summary>
        /// 获取一个值,该值指示 WebBrowser 是否可以在浏览历史记录中向前导航一个页面。
        /// </summary>
        public bool CanGoForward 
        {
            get
            {
                return (_historyStackIndex < _historyStack.Count);
            }
            internal set
            {
                canGoForward = value;
            }
        }

        #endregion
        
        //----------对象的生命周期
        #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

        //----------对象响应的事件
        #region Events

        /// <summary>
        /// 在 WebBrowser 控件已加载内容之后引发的事件。
        /// </summary>
        public event LoadCompletedEventHandler LoadCompleted;
        private void OnLoadCompleted(object sender, NavigationEventArgs e)
        {
            if (LoadCompleted != null)
            {
                LoadCompleted(this, e);
            }
        }

        /// <summary>
        /// 在 WebBrowser 控件成功导航之后引发的事件。
        /// <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>
        /// 当浏览器控件正在导航(包括从重定向)时引发的事件。
        /// </summary>
        public event EventHandler<NavigatingEventArgs> Navigating;
        private void OnNavigating(object sender, NavigatingEventArgs e)
        {
            if (Navigating != null)
            {
                Navigating(this, e);
            }
        }

        /// <summary>
        /// 在 WebBrowser 控件导航失败之后引发的事件。
        /// </summary>
        public event NavigationFailedEventHandler NavigationFailed;
        private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (NavigationFailed != null)
            {
                NavigationFailed(this, e);
            }
        }

        /// <summary>
        /// 当 Javascript 调用 window.external.notify(<data>) 时引发的事件
        /// </summary>
        public event EventHandler<NotifyEventArgs> ScriptNotify;
        private void OnScriptNotify(object sender, NotifyEventArgs e)
        {
            if (ScriptNotify != null)
            {
                ScriptNotify(this, e);
            }
        }

        #endregion

        //----------对象的内部函数
        #region Methods

        /// <summary>
        /// WebBrowser 在浏览历史记录中向后导航一个页面。
        /// </summary>
        public void GoBack()
        {
            if (_historyStackIndex > 1)
            {
                _historyStackIndex -= 1;
                _fromHistory = true;
                _webBrowser.Navigate(_historyStack[_historyStackIndex - 1]);
            }
        }

        /// <summary>
        ///  WebBrowser 在浏览历史记录中向前导航一个页面。
        /// </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>
        /// 应用程序启动“Web 浏览器”应用程序。
        /// </summary>
        public void ShowWebBrowser()
        {
            WebBrowserTask webTask = new WebBrowserTask();
            if ( (this._webBrowser) != null && (this._webBrowser.Source != null))
            {
                webTask.Uri = this._webBrowser.Source;
            }
            webTask.Show();
        }

        #endregion

        //----------对象的依赖属性
        #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
    }
}

    相关评论

    阅读本文后您有什么感想? 已有人给出评价!

    • 8 喜欢喜欢
    • 3 顶
    • 1 难过难过
    • 5 囧
    • 3 围观围观
    • 2 无聊无聊

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)
    推荐文章

    没有数据