Windows Phone 7 网络编程之WebBrowser控件的使用

简介:

WebBrowser 控件可以在应用程序中承载网页以及支持浏览器的其他文档。例如,可以使用 WebBrowser 控件在应用程序中提供基于 HTML 的集成用户帮助或 Web 浏览功能。

WebBrowser控件可以让你的用户浏览一个特定的网页。但它不是一个完整的浏览器,因为它没有地址栏,收藏夹 ,选项卡等等。你可以把它当做HTML中的iframe,但它提供了更丰富的界面。你可以通过两个手指收缩(和双击)来进行缩放,平移和滚动是自动内置的,你无须自己实现。

  这个控件另一个很棒的特性是它可以加载本地和网络中的内容。这意味着如果我有很多HTML文件(也许是文档),那么我不需要为我的应用程序去重新创建这些内容。相反,我可以将这些HTML页面嵌入到我的应用程序中,并在本地(电话中)加载他们而不是依靠一个可能会出现问题的数据连接。

WebBrowser 控件可提供下列功能:

导航SourceNavigateNavigateToStreamNavigateToString 和 Refresh

导航生存期NavigatingNavigated 和 LoadCompleted

导航日记CanGoBackGoBackCanGoForward 和 GoForward

WPF/HTML 互操作InvokeScriptObjectForScripting 和 Document

 

下面的例子WebBrowser控件的简单使用,以及将WebBrowser控件打开的页面保存到手机本地

 

 

 

 
  1. <phone:PhoneApplicationPage   
  2.     x:Class="WindowsPhoneApplication1.MainPage" 
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  7.     xmlns:browser="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  8.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  9.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  11.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  12.     Foreground="{StaticResource PhoneForegroundBrush}" 
  13.     SupportedOrientations="Portrait" Orientation="Portrait" 
  14.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  15.     shell:SystemTray.IsVisible="True"> 
  16.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  17.         <Grid.RowDefinitions> 
  18.             <RowDefinition Height="Auto"/> 
  19.             <RowDefinition Height="*"/> 
  20.         </Grid.RowDefinitions> 
  21.         <Grid x:Name="TitleGrid" Grid.Row="0"> 
  22.             <TextBlock Text="Browser控件测试" x:Name="textBlockPageTitle" Style="{StaticResource PhoneTextNormalStyle}"/> 
  23.         </Grid> 
  24.         <Grid x:Name="ContentGrid" Grid.Row="1"> 
  25.             <browser:WebBrowser Margin="-6,6,12,332" Name="webBrowser1" HorizontalContentAlignment="Left" /> 
  26.             <TextBox Height="90" HorizontalAlignment="Left" Margin="0,427,0,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="319" /> 
  27.             <Button Content="打开网页" Height="70" HorizontalAlignment="Right" Margin="0,427,6,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click"/> 
  28.             <Button Content="把网页保存到本地" Height="72" HorizontalAlignment="Left" Margin="12,503,0,0" Name="btnSave" VerticalAlignment="Top" Width="456" Click="btnSave_Click" /> 
  29.             <Button Content="加载本地保存的页面" Height="72" HorizontalAlignment="Left" Margin="12,581,0,0" Name="btnLoad" VerticalAlignment="Top" Width="456" Click="btnLoad_Click" /> 
  30.         </Grid> 
  31.     </Grid> 
  32.           
  33. </phone:PhoneApplicationPage> 

 

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using System.IO.IsolatedStorage;  
  14. using System.Windows.Resources;  
  15. using System.IO;  
  16.  
  17. namespace WindowsPhoneApplication1  
  18. {  
  19.     public partial class MainPage : PhoneApplicationPage  
  20.     {  
  21.         public MainPage()  
  22.         {  
  23.             InitializeComponent();  
  24.         }  
  25.         private void button1_Click(object sender, RoutedEventArgs e)  
  26.         {  
  27.           webBrowser1.Navigate(new Uri(textBox1.Text, UriKind.Absolute));//在控件中打开网页  
  28.         }  
  29.         private void SaveStringToIsoStore(string strWebContent)  
  30.         {  
  31.             IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();//获取本地应用程序存储对象  
  32.  
  33.             //清除之前保存的网页  
  34.             if (isoStore.FileExists("web.htm") == true)  
  35.             {  
  36.                 isoStore.DeleteFile("web.htm");  
  37.             }  
  38.             StreamResourceInfo sr = new StreamResourceInfo(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(strWebContent)), "html/text");//转化为流  
  39.             using (BinaryReader br = new BinaryReader(sr.Stream))  
  40.             {  
  41.                 byte[] data = br.ReadBytes((int)sr.Stream.Length);  
  42.                 //保存文件到本地存储  
  43.                 using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile("web.htm")))  
  44.                 {  
  45.                     bw.Write(data);  
  46.                     bw.Close();  
  47.                 }  
  48.             }  
  49.         }  
  50.         private void btnSave_Click(object sender, RoutedEventArgs e)  
  51.         {  
  52.             string strWebContent = webBrowser1.SaveToString();//获取网页的html代码  
  53.             SaveStringToIsoStore(strWebContent);  
  54.         }  
  55.  
  56.         private void btnLoad_Click(object sender, RoutedEventArgs e)  
  57.         {  
  58.             webBrowser1.Navigate(new Uri("web.htm", UriKind.Relative));//加载本地保存的页面  
  59.         }  
  60.     }  

 

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078702


相关文章
|
3月前
|
缓存 网络协议 数据安全/隐私保护
[运维笔记] - (命令).Windows server常用网络相关命令总结
[运维笔记] - (命令).Windows server常用网络相关命令总结
191 0
|
1月前
|
Linux iOS开发 MacOS
|
1月前
|
缓存 网络协议 Unix
Windows 命令提示符(CMD)操作(四):网络通信
Windows 命令提示符(CMD)操作(四):网络通信
62 0
|
6月前
|
网络协议 程序员 API
[笔记] Microsoft Windows网络编程《一》WinSock简介(四)
[笔记] Microsoft Windows网络编程《一》WinSock简介(四)
|
4月前
|
网络协议 关系型数据库 网络虚拟化
Windows网络服务综测刷题
Windows网络服务综测刷题
26 0
|
6月前
|
人工智能 网络协议 API
[笔记] Microsoft Windows网络编程《三》网际协议(三)
[笔记] Microsoft Windows网络编程《三》网际协议(三)
|
6月前
|
人工智能 网络协议 API
[笔记] Microsoft Windows网络编程《三》网际协议(二)
[笔记] Microsoft Windows网络编程《三》网际协议(二)
|
6月前
|
网络协议 API 网络架构
[笔记] Microsoft Windows网络编程《三》网际协议(一)
[笔记] Microsoft Windows网络编程《三》网际协议
|
6月前
|
网络协议 API 网络性能优化
[笔记] Microsoft Windows网络编程《二》设计Winsock
[笔记] Microsoft Windows网络编程《二》设计Winsock
|
6月前
|
网络协议 程序员 API
[笔记] Microsoft Windows网络编程《一》WinSock简介(三)
[笔记] Microsoft Windows网络编程《一》WinSock简介(三)