在 .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”类型不包含任何可访问的构造函数。
- 无法向“String”类型的对象添加内容。
- br 在 Windows Presentation Foundation (WPF) 项目中不受支持
- ...(更像那些)
问题是:如何在 WPF 资源字典中存储 HTML 文本,以便不需要额外/隔离的文件,也不会发生语法冲突?
也许 String 不是存储此类内容的最佳类型,在这种情况下还有其他可能吗?
请提供示例。
最佳答案
一切都在转义中,而不是多行
<sys:String x:Key="String1">
This is more sophisticated <br/>
here we have <b>multiple lines</b>
plus some formatting.
</sys:String>
顺便说一句,是 Blend 帮我解决了这个问题。
祝你好运!
关于.net - 如何在 ResourceDictionary 中存储 HTML 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23574763/