现在的位置: 首页 > 综合 > 正文

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

2014年03月11日 ⁄ 综合 ⁄ 共 3989字 ⁄ 字号 评论关闭

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

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

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

WebBrowser 控件可提供下列功能:

导航SourceNavigateNavigateToStreamNavigateToString 和 Refresh

导航生存期NavigatingNavigated 和 LoadCompleted

导航日记CanGoBackGoBackCanGoForward 和 GoForward

WPF/HTML 互操作InvokeScriptObjectForScripting 和 Document

 

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

 

 

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Windows.Resources;
using System.IO;

namespace WindowsPhoneApplication1
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
          webBrowser1.Navigate(new Uri(textBox1.Text, UriKind.Absolute));//在控件中打开网页
        }
        private void SaveStringToIsoStore(string strWebContent)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();//获取本地应用程序存储对象

            //清除之前保存的网页
            if (isoStore.FileExists("web.htm") == true)
            {
                isoStore.DeleteFile("web.htm");
            }
            StreamResourceInfo sr = new StreamResourceInfo(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(strWebContent)), "html/text");//转化为流
            using (BinaryReader br = new BinaryReader(sr.Stream))
            {
                byte[] data = br.ReadBytes((int)sr.Stream.Length);
                //保存文件到本地存储
                using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile("web.htm")))
                {
                    bw.Write(data);
                    bw.Close();
                }
            }
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string strWebContent = webBrowser1.SaveToString();//获取网页的html代码
            SaveStringToIsoStore(strWebContent);
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            webBrowser1.Navigate(new Uri("web.htm", UriKind.Relative));//加载本地保存的页面
        }
    }
}

抱歉!评论已关闭.