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));//加载本地保存的页面
}
}
}