【问题标题】:How to store an HTML string in a ResourceDictionary?如何在 ResourceDictionary 中存储 HTML 字符串?
【发布时间】:2014-06-27 19:01:39
【问题描述】:

在 .NET (WPF) 中,我们可以轻松地将简单的字符串存储在资源字典中,因此请考虑下一个示例...

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <sys:String x:Key="PlainText">This is just a simple line of text</sys:String>

    <sys:String x:Key="FormattedText">This is more sophisticated</br>
         here we have <strong>multiple lines</strong> of text</br>
         plus some <strong>formatting</strong>.
    </sys:String>

</ResourceDictionary>

第一个字符串(带有“PlainText”键)完全合法且工作正常。

但是,第二个字符串(带有键“FormattedText”)不被接受,因为它使用多行和 HTML 语法与 XAML 语法冲突,因此 Visual Studio (2013) 显示以下错误消息:

  • “String”类型不包括任何可访问的构造函数。
  • 无法向“字符串”类型的对象添加内容。
  • br 在 Windows Presentation Foundation (WPF) 项目中不受支持
  • ....(更像那些)

问题是:如何将 HTML 文本存储在 WPF 资源字典中,这样就不需要额外/孤立的文件,也不会发生语法冲突?

也许 String 不是存储此类内容的最佳类型,在这种情况下,还有哪些可能呢?

请举个例子。

【问题讨论】:

标签: .net wpf xaml resourcedictionary


【解决方案1】:

都是转义,不是多行

<sys:String x:Key="String1">
    This is more sophisticated &lt;br/&gt;
    here we have &lt;b&gt;multiple lines&lt;/b&gt;
    plus some formatting.
</sys:String>

顺便说一句,是 Blend 帮我解决了这个问题。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    相关资源
    最近更新 更多