Windows Phone 8 的 WebBrowser 控件

2025-10-20 10:24:01

1、继承层次结构

System.Object  System.Windows.DependencyObject    System.Windows.UIElement      System.Windows.FrameworkElement        System.Windows.Controls.Control          Microsoft.Phone.Controls.WebBrowserBase            Microsoft.Phone.Controls.WebBrowser

2、属性

Windows Phone 8 的 WebBrowser 控件

3、方法

Windows Phone 8 的 WebBrowser 控件

4、事件:

Windows Phone 8 的 WebBrowser 控件

5、程序代码

xaml

<phone:PhoneApplicationPage 

    x:Class="WebBrowserTest.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:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:browser="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>

            <TextBlock x:Name="PageTitle" Text="WebBrowser控件测试" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <browser:WebBrowser Margin="-6,6,12,332" Name="webBrowser1" HorizontalContentAlignment="Left" />

            <TextBox Height="90" HorizontalAlignment="Left" Margin="6,331,0,0" Name="textBox1" Text="http://www.baidu.com/" VerticalAlignment="Top

            <Button Content="打开网页" Height="70" HorizontalAlignment="Right" Margin="0,331,-12,0" Name="button1" VerticalAlignment="Top

            <Button Content="把网页保存到本地" Height="72" HorizontalAlignment="Left" Margin="12,427,0,0" Name="btnSave" VerticalAlignment="Top

            <Button Content="加载本地保存的页面" Height="72" HorizontalAlignment="Left" Margin="12,505,0,0" Name="btnLoad" VerticalAlignment="Top

        </Grid>

    </Grid>

</phone:PhoneApplicationPage>

.cs

using System;

using System.Windows;

using Microsoft.Phone.Controls;

using System.IO.IsolatedStorage;

using System.Windows.Resources;

using System.IO;

namespace WebBrowserTest

{

    public partial class MainPage : PhoneApplicationPage

    {

        public MainPage()

        {

            InitializeComponent();

        }

        /// <summary>

        /// 单击打开网页按钮事件处理

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            //在控件中打开网页

            webBrowser1.Navigate(new Uri(textBox1.Text, UriKind.Absolute));

        }

        /// <summary>

        /// 保存网页到本地的独立存储

        /// </summary>

        /// <param name="strWebContent"></param>

        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();

                }

            }

        }

        /// <summary>

        /// 把网页保存到本地按钮事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnSave_Click(object sender, RoutedEventArgs e)

        {

            string strWebContent = webBrowser1.SaveToString();//获取网页的html代码

            SaveStringToIsoStore(strWebContent);

        }

        /// <summary>

        /// 加载本地保存的页面

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnLoad_Click(object sender, RoutedEventArgs e)

        {

            webBrowser1.Navigate(new Uri("web.htm", UriKind.Relative));//加载本地保存的页面

        }

    }

}

6、运行结果

Windows Phone 8 的 WebBrowser 控件

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢