Question

In .NET (WPF) we can easily store simple strings in resource dictionaries, so please consider the next example...

<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>

The first string (with key "PlainText") is perfectly legal and works fine.

However, the second string (with key "FormattedText") is not accepted because uses multiple lines and syntax of HTML colliding with XAML syntax, so Visual Studio (2013) shows the next error messages:

  • The type "String" does not include any accessible constructors.
  • Cannot add content to an object of type "String".
  • br is not supported in a Windows Presentation Foundation (WPF) Project
  • .... (more like those)

Question is: How to store HTML texts in a WPF Resource Dictionary so that no extra/isolated files are required nor syntax collision happens?

Maybe String is not the best type to store this type of content, in that case which else could be?

Please provide an example.

Was it helpful?

Solution

It's all in the escaping, not the multiple lines

<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>

By the way, it was Blend that solved this for me.

Best of luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top