在WP7应用程序中显示富文本

3

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

2个回答

2
如果您想要显示一个HTML页面或文件,您应该使用WebBrowser控件。它支持所有基本功能,您可以从一个网络浏览器中期望到的;html标记、样式、锚点跳转到其他资源或页面内位置。
如果要显示位于Visual Studio项目中的文件,则需要像这样做。如果您需要更多信息,请告诉我。希望对您有所帮助。
Al.
/// <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
我想避免使用WebBrowser控件,因为它对我来说有点过头了... - Hinek
嗯……能再详细解释一下吗?从代码方面来说非常干净,你担心创建此控件会导致内存和CPU负载过高吗? - ajmccall
就像我说的,只是一种感觉。我现在尝试了WebBrowser,但无法显示页面。我将HTML文件添加到项目中并将其标记为嵌入式资源。然后我将源设置为“/MyApp;component/About.html”...我还尝试使用Navigate方法和RelativeUri...但都没有成功。我的错误在哪里? - Hinek
WebControl无法显示XAP捆绑的文件,只能显示IsolateFileStorage中的文件。第一次想要显示本地HTML文件时,需要从捆绑资源中“复制”该文件并将其“保存”到手机的本地文件系统(IsolatedFileStorage)。从那时起,Navigate方法将按预期工作。我已经包含了我的代码,这是一个扩展方法,用于执行此操作。否则,可以尝试@Nigel提出的答案。 - ajmccall
使用HTML和CSS在Web浏览器控件中非常灵活且易于使用,即使对于初学者也是如此。 - Edward
WebControl的一个大问题是背景总是白色的,而且你无法修改背景颜色。 - phuongho

2

Windows Phone的Mango版本将Silverlight版本从3升级到4。作为其中的一部分,他们引入了RichTextBox控件,该控件可能会满足您的需求。

有一篇关于此控件的文章(尽管有些陈旧),链接如下:First Look at RichTextBox Control


1
是的,您需要从http://create.msdn.com下载最新的SDK,一旦安装完成,您的项目在解决方案资源管理器中右键单击时将有一个“升级”选项。 - Nigel Sampson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接