【发布时间】:2011-10-11 06:49:34
【问题描述】:
我想在我的 WP7 应用中显示关于文本。但它包含链接、粗体文本和项目符号列表。是否有一种简单的方法可以将其显示为某种富文本或 html?我不想使用带有文本块和超链接的堆栈面板来构建它...
【问题讨论】:
标签: windows-phone-7 silverlight-4.0 silverlight-toolkit
我想在我的 WP7 应用中显示关于文本。但它包含链接、粗体文本和项目符号列表。是否有一种简单的方法可以将其显示为某种富文本或 html?我不想使用带有文本块和超链接的堆栈面板来构建它...
【问题讨论】:
标签: windows-phone-7 silverlight-4.0 silverlight-toolkit
Windows Phone 的 Mango 版本将 Silverlight 版本从 3 提升到 4。作为其中的一部分,他们引入了 RichTextBox 控件,可以满足您的需求。 '
一篇关于First Look at RichTextBox Control的文章(诚然是旧的)。
【讨论】:
如果您要显示 HTML 页面或文件,则应使用 WebBrowser 控件。它支持您期望从网络浏览器获得的所有基本功能; html 标记、样式、锚标记跳转到页面中的其他资源或位置。
要显示位于 Visual Studio 项目中的文件,您需要执行this 之类的操作。如果您需要更多信息,请告诉我。希望这会有所帮助。
阿尔。
=== 更新 ===
/// <summary>
/// Contains extension methods for the WebBrowser control.
/// </summary>
public static class WebBrowserExtensions {
private static void SaveFileToIsoStore(String fileName) {
//These files must match what is included in the application package,
//or BinaryStream.Dispose below will throw an exception.
using(IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
if (false == isoStore.FileExists(fileName)) {
StreamResourceInfo sr = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
using (BinaryReader br = new BinaryReader(sr.Stream)) {
byte[] data = br.ReadBytes((int)sr.Stream.Length);
SaveToIsoStore(fileName, data);
}
}
}
}
private static void SaveToIsoStore(string fileName, byte[] data) {
string strBaseDir = string.Empty;
string delimStr = "/\\";
char[] delimiter = delimStr.ToCharArray();
string[] dirsPath = fileName.Split(delimiter);
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
//Recreate the directory structure
for (int i = 0; i < dirsPath.Length - 1; i++) {
strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
isoStore.CreateDirectory(strBaseDir);
}
//Remove existing file
if (isoStore.FileExists(fileName)) {
isoStore.DeleteFile(fileName);
}
//Write the file
using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName))) {
bw.Write(data);
bw.Close();
}
}
}
public static void NavigateToHtmlFile(this WebBrowser webBrowser, String fileName) {
SaveFileToIsoStore(fileName);
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
if (isoStore.FileExists(fileName)) {
webBrowser.Navigate(new Uri(fileName, UriKind.Relative));
} else {
//something bad has happened here
}
}
}
}
然后在你的 xaml 中
MyWebControl.NavigateToHtmlFile(pathToHtmlFile);
【讨论】: