【问题标题】:Display Richtext in WP7 app在 WP7 应用中显示富文本
【发布时间】:2011-10-11 06:49:34
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    一篇关于First Look at RichTextBox Control的文章(诚然是旧的)。

    【讨论】:

    • 是的,您需要从create.msdn.com 下载最新的SDK,安装后,您的项目将在解决方案资源管理器中右键单击“升级”选项。
    【解决方案2】:

    如果您要显示 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);
    

    【讨论】:

    • 我想避免使用 WebBrowser 控件,因为我觉得它有点过头了......
    • hmmmmmm...解释得有点过头了?代码方面非常干净,您是否担心创建此控件的内存/CPU 开销?
    • 正如我所说,只是一种感觉。我现在尝试了 WebBrowser,但没有显示页面。我将 html 文件添加到项目中并将其标记为 Embedded Ressource。然后我将 Source 设置为 "/MyApp;component/About.html" ... 我还尝试使用 Navigate 方法和 RelativeUri ... 无济于事。我的错在哪里?
    • WebControl 不能显示捆绑在 XAP 中的文件,只能显示 IsolateFileStorage 上的文件。第一次显示本地 html 文件时,您需要从捆绑资源中“复制”该文件并将其“保存”到手机的本地文件系统 (IsolatedFileStorage)。从那里开始,导航方法将按预期工作。我已经包含了我的代码,这是一种扩展方法,可以做到这一点。否则请随意尝试@Nigel提出的答案
    • +1 在 Web 浏览器控件中使用 HTML 和 CSS 非常灵活且易于使用,即使对于初学者也是如此。
    猜你喜欢
    • 1970-01-01
    • 2020-08-19
    • 2018-06-21
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多