在WP7应用中显示富文本

时间:2011-10-11 06:49:35

标签: windows-phone-7 silverlight-4.0 silverlight-toolkit

我想在我的WP7应用中显示一个关于文本的内容。但它包含链接,粗体文本和项目符号列表。是否有简单的方法将其显示为某种richtext或html?我不想使用带有文本块和超链接的堆栈面板来构建它...

2 个答案:

答案 0 :(得分:2)

如果要显示HTML页面或文件,则应使用WebBrowser控件。它支持您期望从Web浏览器获得的所有基本功能; html标记,样式,锚标签跳转到您页面中的其他资源或位置。

要显示位于Visual Studio项目中的文件,您需要执行this之类的操作。如果您需要其他信息,请与我们联系。希望这会有所帮助。

的Al。

=== updated ===

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

答案 1 :(得分:2)

Windows Phone的芒果版本将Silverlight版本从3提升到4.作为其中的一部分,他们引入了RichTextBox控件,可以满足您的需要。 “

一篇文章(老实说)是First Look at RichTextBox Control