WP7 Browser Control WebBrowser history, forward, rewind, refresh function implementation

Source: Internet
Author: User
Tags rewind

Do not jump out of the app because you want to access the Web page inside the app. So to implement a browser. But it's a sad thing. Windows Phone8 's WebBrowser controls already support these features like CanGoBack, Cangoforward,goback,goforwarD, But WP7 did not implement a few forward-and-backward functions. It is also very simple to use in the page, the implementation of the effect as shown.

Because the use is very simple. Is the same as WebBrowser on Windows Phone8. I'll just give the code to the control, and I'll use the code for the post.

The code is as follows , and the comments are clearly written.

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&gt        ;        Gets the ControlTemplate string for the control. </summary>//<remarks>//Not in generic.xaml so the implementation of Hcwebview can be E        Ntirely in the This file. </remarks> private static string Templatestring {get {retur N "<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>";        }}//----------internal members of the object #region Member//<summary>//WebBrowser.        </summary> private WebBrowser _webbrowser;        <summary>///History Uri record stack///</summary> private list<uri> _historystack;        <summary>///History stack index///</summary> private int _historystackindex;        <summary>///The URI to navigate to is from the history stack///</summary> private bool _fromhistory; private boolCanGoBack;        <summary>//////Gets a value that indicates whether WebBrowser can navigate a page forward in the browsing history. </summary> public bool CanGoBack {get {return (_HISTORYSTAC            Kindex > 1);            } internal set {cangoback = value;        }} private bool CanGoForward;        <summary>//////Gets a value that indicates whether WebBrowser can navigate a page forward in the browsing history. </summary> public bool CanGoForward {get {return (_history            Stackindex < _historystack.count);            } internal set {CanGoForward = value;        }} #endregion//----------life cycle of objects #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//----------object responds to Events #region events///<summary>//In WebB        The event that is raised after the Rowser control has loaded content.        </summary> public event Loadcompletedeventhandler loadcompleted;            private void Onloadcompleted (object sender, NavigationEventArgs e) {if (loadcompleted! = null)            {loadcompleted (this, e);        }}///<summary>///The event raised after the WebBrowser control successfully navigated.        <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>///The event that is raised when the browser control is navigating (including from redirection).        </summary> public event eventhandler<navigatingeventargs> Navigating;                private void Onnavigating (object sender, Navigatingeventargs e) {if (Navigating! = null) {            Navigating (this, e);        }}///<summary>///event raised after WebBrowser control navigation failed.        </summary> public event Navigationfailedeventhandler navigationfailed; private void Onnavigationfailed (object sender, Navigationfailedeventargs e) {if (navigationfailed! = Nu ll) {NaviGationfailed (this, e); }}////<summary>///when Javascript invokes Window.external.notify (<data>), the event that is raised///&L        T;/summary> public event eventhandler<notifyeventargs> Scriptnotify;                private void Onscriptnotify (object sender, Notifyeventargs e) {if (scriptnotify! = null) {            Scriptnotify (this, e); }} #endregion//----------The intrinsic function of the object #region Methods//<summary>//WEBBR        Owser navigates backward through a page in the browsing history.                </summary> public void GoBack () {if (_historystackindex > 1) {                _historystackindex-= 1;                _fromhistory = true;            _webbrowser.navigate (_historystack[_historystackindex-1]);        }}///<summary>///WebBrowser navigates a page forward in the browsing history.       </summary> public void GoForward () {     if (_historystackindex < _historystack.count) {_historystackindex + = 1;                _fromhistory = true;            _webbrowser.navigate (_historystack[_historystackindex-1]); }}///<summary>//Refresh Hcwebview//</summary> public void REFRESHW                Ebview () {if (this._webbrowser) = null && (This._webbrowser.source! = null)) {            This._webbrowser.source = new Uri (this._webbrowser.source.tostring ());        }}///<summary>//The application launches the Web browser application. </summary> public void ShowWebBrowser () {Webbrowsertask webtask = new Webbrowsertask ()            ; if (this._webbrowser) = null && (This._webbrowser.source! = null)) {Webtask.uri = th            Is._webbrowser.source;        } webtask.show (); } #endregion//----------The dependency property of the object #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.regis ter ("Title", typeof (Uri), typeof (Hcwebview), new PropertyMetadata (NULL, New Propertychangedcallback (onsourcechanged)        )); <summary>//Prevents the Webwiew Source from transitioning to a semiexpanded or collapsed visual stat        E 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) {Hcweb            View WebView = (hcwebview) obj;            if (e.newvalue! = null) {Webview._webbrowser.source = E.newvalue as Uri; }} #endregion #endregion}}

Translated from: http://www.cr173.com/html/19834_1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.