Windows Phone 提供基於桌面瀏覽器的 WebBrowser 控制元件。Windows Phone OS 7.1 的 WebBrowser 控制元件基於 Internet Explorer 9,Windows Phone 8 的 WebBrowser 控制元件基於 Internet Explorer 10。因此,與在 Windows Phone 8 上執行的手機相比,使用 Windows Phone 7.5 上執行的 WebBrowser 控制元件的應用,其外觀稍有不同。
WebBrowser 控制元件可以嵌入應用中並且其用途十分廣泛,包括但不限於:
顯示網路中的 Web 內容。您可以構建一個應用,該應用只是由指向您網站的嵌入 WebBrowser 控制元件組成,在該控制元件外面有自定義標記。有關更多資訊,請參見如何使用 Windows Phone 8 的 WebBrowser 控制元件顯示網路中的 Web 內容。
顯示靜態的 Web 內容。可以將應用配置為將內容儲存在本地獨立儲存,然後使用者就可以在嵌入的WebBrowser 控制元件中檢視這些內容。有關更多資訊,請參見如何使用 Windows Phone 8 的 WebBrowser 控制元件顯示靜態 Web 內容。
顯示動態生成的 Web 內容。可以將 WebBrowser 控制元件指向應用程式碼中動態構造的 HTML 內容。有關更多資訊,請參見如何使用 Windows Phone 8 的 WebBrowser 控制元件顯示動態生成的 Web 內容。
工具/原料
MS visual studio Express 2012 for windows phone
win8 專業版 64位
方法/步驟
繼承層次結構
System.Object System.Windows.DependencyObject System.Windows.UIElement System.Windows.FrameworkElement System.Windows.Controls.Control Microsoft.Phone.Controls.WebBrowserBase Microsoft.Phone.Controls.WebBrowser
屬性
方法
事件:
程式程式碼
xaml
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">
.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();
}
///
/// 單擊開啟網頁按鈕事件處理
///
///
///
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));//載入本地儲存的頁面
}
}
}
執行結果
注意事項
儲存到獨立儲存檔案
本地載入