设为首页 收藏本站
查看: 1005|回复: 0

Windows Phone 7 浏览器控件(WebBrowser) 支持历史记录,前进,后退,刷新。等功能

[复制链接]

尚未签到

发表于 2015-5-9 09:51:42 | 显示全部楼层 |阅读模式
  由于要在应用程序内部访问网页。不跳出应用。所以要实现一个浏览器。但是悲催的事windows phone8 的
  WebBrowser控件已经支持了像CanGoBack ,CanGoForward,GoBack,GoForward等这些功能。但是
  wp7没有。网上搜了搜也都是简单的WebBrowser的使用方法。暂时就实现了几个前进后退几个功能。用在
  page里面也很简单。实现的效果如下图所示。
DSC0000.png
  因为使用十分简单。和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
{
///
/// Gets the ControlTemplate string for the control.
///
///
/// Not in generic.xaml so the implementation of HcWebView can be entirely in this file.
///
private static string TemplateString
{
get
{
return
"" +
"" +
"" +
"" +
"";
}
}
//----------对象的内部成员
#region Member
///
/// WebBrowser.
///
private WebBrowser _webBrowser;
///
/// 历史Uri记录堆栈
///
private List _historyStack;
///
/// 历史记录堆栈索引
///
private int _historyStackIndex;
///
/// 导航到的Uri是否来自历史堆栈
///
private bool _fromHistory;
private bool canGoBack;
///
/// 获取一个值,该值指示 WebBrowser 是否可以在浏览历史记录中向前导航一个页面。
///
public bool CanGoBack
{
get
{
return (_historyStackIndex > 1);
}
internal set
{
canGoBack = value;
}
}
private bool canGoForward;
///
/// 获取一个值,该值指示 WebBrowser 是否可以在浏览历史记录中向前导航一个页面。
///
public bool CanGoForward
{
get
{
return (_historyStackIndex < _historyStack.Count);
}
internal set
{
canGoForward = value;
}
}
#endregion
//----------对象的生命周期
#region LifeCycle
///
/// Initializes a new instance of the HcWebView class.
///
public HcWebView()
{
this._historyStack = new List();
this._historyStackIndex = 0;
this._fromHistory = false;
this.Template = (ControlTemplate)XamlReader.Load(TemplateString);
}
///
/// Invoked when a new Template is applied.
///
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
///
/// 在 WebBrowser 控件已加载内容之后引发的事件。
///
public event LoadCompletedEventHandler LoadCompleted;
private void OnLoadCompleted(object sender, NavigationEventArgs e)
{
if (LoadCompleted != null)
{
LoadCompleted(this, e);
}
}
///
/// 在 WebBrowser 控件成功导航之后引发的事件。
///
public event EventHandler 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);
}
}
///
/// 当浏览器控件正在导航(包括从重定向)时引发的事件。
///
public event EventHandler Navigating;
private void OnNavigating(object sender, NavigatingEventArgs e)
{
if (Navigating != null)
{
Navigating(this, e);
}
}
///
/// 在 WebBrowser 控件导航失败之后引发的事件。
///
public event NavigationFailedEventHandler NavigationFailed;
private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (NavigationFailed != null)
{
NavigationFailed(this, e);
}
}
///
/// 当 Javascript 调用 window.external.notify() 时引发的事件
///
public event EventHandler ScriptNotify;
private void OnScriptNotify(object sender, NotifyEventArgs e)
{
if (ScriptNotify != null)
{
ScriptNotify(this, e);
}
}
#endregion
//----------对象的内部函数
#region Methods
///
/// WebBrowser 在浏览历史记录中向后导航一个页面。
///
public void GoBack()
{
if (_historyStackIndex > 1)
{
_historyStackIndex -= 1;
_fromHistory = true;
_webBrowser.Navigate(_historyStack[_historyStackIndex - 1]);
}
}
///
///  WebBrowser 在浏览历史记录中向前导航一个页面。
///
public void GoForward()
{
if (_historyStackIndex < _historyStack.Count)
{
_historyStackIndex += 1;
_fromHistory = true;
_webBrowser.Navigate(_historyStack[_historyStackIndex - 1]);
}
}
///
/// Refresh HcWebView
///
public void RefreshWebView()
{
if ((this._webBrowser) != null && (this._webBrowser.Source != null))
{
this._webBrowser.Source = new Uri(this._webBrowser.Source.ToString());
}
}
///
/// 应用程序启动“Web 浏览器”应用程序。
///
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
///
/// Gets or sets the Source.
///
public Uri Source
{
get { return (Uri)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
///
/// Identifies the Source dependency property.
///
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Title", typeof(Uri), typeof(HcWebView), new PropertyMetadata(null, new PropertyChangedCallback(OnSourceChanged)));
///
/// Prevents the webWiew Source from transitioning into a Semiexpanded or Collapsed visual state if the Source is not set.
///
/// The dependency object.
/// The event information.
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
}
}
  
  
  谢谢阅读。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-65163-1-1.html 上篇帖子: Windows Phone 7(WP7)开发 自订磁贴(深度链接) 下篇帖子: Windows Phone 7 不温不火学习之《使用Expression Blend 创建应用程序栏》
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表