在开发Windows Phone 7应用程序时,经常需要为用户提供帮助文档。这些文档通常包含按钮说明、屏幕响应等信息,并且最好以图像的形式展示。本文将介绍如何将HTML页面和相关图像存储在Isolated Storage中,并在应用程序中显示这些帮助页面。
Windows Phone 7应用程序中,Browser控件只能通过Isolated Storage机制访问本地HTML内容。本文介绍的方法就是基于这一机制,将HTML页面和图像文件移动到Isolated Storage中,并在应用程序中进行检索和显示。
示例应用程序包含三个页面:
列表页面使用ListPicker控件展示州的列表,当选中某个州时,该州的名称会显示在TextBlock中。
以下是设置ListPicker控件的代码示例:
void ListPage_Loaded(object sender, RoutedEventArgs e)
{
StatesListPicker.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(StatesListPicker_SelectionChanged);
StatesListPicker.SelectedIndex = 4;
}
void StatesListPicker_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
StateNameTextBlock.Text = StatesListPicker.SelectedItem.ToString();
}
当ListPicker项更改时,州名称会显示在TextBlock中。
图像按钮的灵感来源于Przemyslaw Chruscicki的文章《ImageButton control forWindows Phone 7: Silverlight and Windows Phone 7 Geek Page》。以下是设置按钮的代码示例:
ImageButton控件模板位于App.xaml.cs中。当按钮被按下时,会执行以下按钮事件,导航用户到选中的页面:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button_selected = sender as Button;
string button_name = button_selected.Name;
string uri = "";
switch (button_name)
{
case "List_Button":
uri = "/ListPage.xaml";
break;
case "Help_Button":
uri = "/HelpPage.xaml";
break;
case "About_Button":
uri = "/AboutPage.xaml";
break;
default:
break;
}
try
{
NavigationService.Navigate(new Uri(uri, UriKind.RelativeOrAbsolute));
}
catch (Exception enav)
{
string serror = enav.Message;
}
}
帮助页面和关于页面是本文的主要主题。这些页面使用Browser控件显示本地HTML页面。例如,帮助页面的显示代码如下:
void HelpPage_Loaded(object sender, RoutedEventArgs e)
{
webBrowser1.Base = "HTDocs";
webBrowser1.Navigate(new Uri("Help.htm", UriKind.Relative));
}
代码非常简单,只需设置基础目录为HTDocs,然后导航到HTML页面即可。
在应用程序启动时,会调用Initialize_Help_HTMLPage()方法。以下是设置HTML页面的代码示例:
private void Initialize_Help_HTMLPage()
{
storageFile.CreateDirectory("HTDocs\\images");
storageFile.CopyTextFile("HTDocs\\Help.htm", true);
storageFile.CopyTextFile("HTDocs\\About.htm", true);
storageFile.CopyBinaryFile("HTDocs\\images\\About48.png", true);
storageFile.CopyBinaryFile("HTDocs\\images\\Help48.png", true);
storageFile.CopyBinaryFile("HTDocs\\images\\list48.png", true);
storageFile.CopyBinaryFile("HTDocs\\images\\ListPicker.png", true);
storageFile.CopyBinaryFile("HTDocs\\images\\PickedState.png", true);
}
在Isolated Storage中创建HTDocs目录,并将HTML文件和图像复制到这个目录中。使用Visual Studio创建了这个目录、HTML文件和图像目录,并复制了图像文件。请确保图像文件的类型是Content而不是Resource。
public static class ISExtensions
{
public static void CopyTextFile(this IsolatedStorageFile isf, string filename, bool replace = false)
{
if (!isf.FileExists(filename) || replace == true)
{
StreamReader stream = new StreamReader(TitleContainer.OpenStream(filename));
IsolatedStorageFileStream outFile = isf.CreateFile(filename);
string fileAsString = stream.ReadToEnd();
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(fileAsString);
outFile.Write(fileBytes, 0, fileBytes.Length);
stream.Close();
outFile.Close();
}
}
public static void CopyBinaryFile(this IsolatedStorageFile isf, string filename, bool replace = false)
{
if (!isf.FileExists(filename) || replace == true)
{
BinaryReader fileReader = new BinaryReader(TitleContainer.OpenStream(filename));
IsolatedStorageFileStream outFile = isf.CreateFile(filename);
bool eof = false;
long fileLength = fileReader.BaseStream.Length;
int writeLength = 512;
while (!eof)
{
if (fileLength < 512)
{
writeLength = Convert.ToInt32(fileLength);
outFile.Write(fileReader.ReadBytes(writeLength), 0, writeLength);
}
else
{
outFile.Write(fileReader.ReadBytes(writeLength), 0, writeLength);
}
fileLength = fileLength - 512;
if (fileLength <= 0) eof = true;
}
fileReader.Close();
outFile.Close();
}
}
}