Multi-line strings in ResourceDictionary.xaml

K

Kirill

Hello,

For those, who read this newsgroup via the browser: I'm sorry about the
repost, but the post does not appear to be on the newsgroup server, and that
could be a reason that I've not received *any* replies.

How do I specify a string with line breaks in a resource dictionary?

Here is what I want:
I'd like to put a string that contains line breaks (or other special
entities), e.g.
or \n, into a resource dictionary. In other words, I'd
like my resouce dictionary to look like:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
<System:String x:Key="TwoLiner">First line
Second line</System:String>
</ResourceDictionary>


Here is the problem:
when I retrieve that string with

ResourceDictionary _main = (ResourceDictionary)Application.LoadComponent(new
Uri("WpfCustomControlLibrary2;;;component/CustomDictionary.xaml",
UriKind.Relative));
string twoLiner = _main["TwoLiner"] as string;

the line break (
in .xaml) is replaced with ' ' (\32 in C#)


Here is what I found:
- I tried to use a workaround to a similar problem described in
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/621b9169-94d0-4217-808d-e3adbe92d448/
but I could not make it work and always received internal compiler error
'Object reference not set to an instance of an object.' unless I used
attribute syntax like

<src:ResourceString x:Key="TwoLiner" Value="First line
Second line" />

- to solve the internal compiler error, I tried to write a TypeConverter
that can convert from string to ResourceString, but the error still appears.


Although it's somewhat working, I'd like to:
- either use System:String;
- or use ResourceString but specify the string in the dictionary in the
usual - content - way (not as an attribute).

Thank you for any help or ideas!

Kirill.
 
M

Marco Zhou [MSFT]

Hello Kirill,

Welcome to Microsoft Newsgroup Support Service! My name is Marco Zhou. It's
my pleasure to work with you on this thread.

If I understand your question correctly, you need to define pure
System.String resource in XAML, if this is the case, you could try
something like the following snippet:

<Window.Resources>
<System:String x:Key="TwoLiner" xml:space="preserve">First
line
Second line</System:String>
</Window.Resources>

The xml:space attribute here means that we want to preserve the white space
when parsing, and "
" XML entity means "\n"(aka newline character).

For more information on whitespace handling in XAML, please refer to the
following article:
http://msdn.microsoft.com/en-us/library/ms788746.aspx

If you have any further questions on this issue, free feel to ask here, we
are glad to answer them.

--------------------------------------------------
Best regards,
Macro Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Kirill

Marco,
If I understand your question correctly, you need to define pure
System.String resource in XAML,
Yes, that's exactly what I wanted to do. The trick is I wanted to have line
breaks (aka \n, aka
) within those pure strings.
if this is the case, you could try something like the following snippet:

<Window.Resources>
<System:String x:Key="TwoLiner" xml:space="preserve">First
line
Second line</System:String>
</Window.Resources>

The xml:space attribute here means that we want to preserve the white
space
when parsing, and "
" XML entity means "\n"(aka newline character).

For more information on whitespace handling in XAML, please refer to the
following article:
http://msdn.microsoft.com/en-us/library/ms788746.aspx
Thank you so much! You managed to remove a lot of code that you haven't even
seen :)
 
A

Ashutosh Bhawasinka

Apart from that...if you are using the Resource editor (in the IDE) you
can first type your multi-line text in application like notepad then
copy and paste it in the resource editor....Thats how I do!!!

Thanks & Regards,
Ashutosh Bhawasinka
 
K

Kirill

Ashutosh Bhawasinka,
Apart from that...if you are using the Resource editor (in the IDE) you
can first type your multi-line text in application like notepad then copy
and paste it in the resource editor....Thats how I do!!!
Unfortunately, we can't use Resource editor, because it edits .resx files,
not .xaml files. But if one uses Resource editor (and .resx files), then
your hint is wonderful!

Kirill.
 
A

Ashutosh Bhawasinka

Yes, you are correct!!! thats why added the phrase "Apart from that" at
the beginning of my sentence :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top