How can I use a StaticResource within a <system:String> on a ResourceDictionary

Dear Experts-Exchange:

I am starting to translate the Resource Dictionaries of my application and noticed that I am using the same phrase in several places.

I would like to know if it's possible to create a <system:String> entry that I could reuse within other <system:String> like in the following ResourceDictionary example:

<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="PleasePressOK">Please press 'OK' to continue.</system:String>
	
	<system:String x:Key="Error1">Invalid value.  {StaticResource PleasePressOK}</system:String>
	<system:String x:Key="Error2">Access Denied.  {StaticResource PleasePressOK}</system:String>
	
</ResourceDictionary>

Open in new window


I will appreciate your feedback.

Best regards,

Conrado
Conrado ZAVALAAsked:
Who is Participating?
 
apeterConnect With a Mentor Commented:
Syntax wise it is correct. it is not going to give error. "Invalid value.  {StaticResource PleasePressOK}" will be considered as one whole string. This, {StaticResource PleasePressOK} should be assigned to a property of a control. for e.g... "Text" property of a TextBox.    

If you want to combine, the value of two resource strings, you have to do in code behind and assign the concatenated string to a element/control.
0
 
ambienceConnect With a Mentor Commented:
There are several problems.

<system:String x:Key="Error1">Invalid value.  {StaticResource PleasePressOK}</system:String>

Open in new window

"{StaticResource PleasePressOK}" is for use as Attribute value, should you need to use it as element value the correct syntax is

<SomeNode>
     <StaticResource ResourceKey="PleasePressOK" />
</SomeNode>

Open in new window


This however, is not really a problem because it doesnt work even if you use the correct XAML syntax:

<system:String x:Key="Error1">
     Invalid value.  
    <StaticResource ResourceKey="PleasePressOK" />
</system:String>

Open in new window


If the object wasn't STRING one could use the property-element syntax like

<Button x:Key="Error1">
     <Button.Content>
     Invalid value.  
    <StaticResource ResourceKey="PleasePressOK" />
     </Button.Content>
</Button>

Open in new window


1 - String however, is immutable and does not have any property that can be assigned to AFTER it has been constructed with default constructor, which the XAML parser would do.

2 - Still, this will not work because the value

     Invalid value.  
    <StaticResource ResourceKey="PleasePressOK" />

is treated as collection of two values and XAML doesnt know how to handle it unless the property being assigned to is IList. This works for example:

	<x:Array Type="system:String" x:Key="Error1">
		Invalid value. 
		<StaticResource ResourceKey="PleasePressOK" />
	</x:Array>

Open in new window


As you can see there is no easy way to enforce the given syntax and make it work as desired. As for the solution, you can

1 - Write a custom Resource Markup extension, that parses the syntax you have used and converts it into a String. You can then use <Button Content={local:MyStringResource Error1}"/>.

2 - Define your strings as array of string and write a markup extension to fetch it from resources, concatenate and return. This would be more type-safe compared to 1, at the cost of increased complexity of defining strings. A type convertor would also do, but if only you use Binding extension or any other extension that is aware of type conversion. Check the CatExtension from https://github.com/MrCircuit/XAMLMarkupExtensions, which allows for concatenation of String.

3 - Write a wrapper Class around StringBuilder that has a default constructor and a property that acts like a collection, so that you can use the property-element syntax to all items to it. Internally, each addition would concatenate (like StringBuilder). Also provide conversion to String. This allows for convenient definition of Strings as well as usage.

Disclaimer: #3, I haven't tested myself, but should work
0
 
Conrado ZAVALAAuthor Commented:
Thank you 'apeter' and 'ambience'.  In other words, there is no way to define once a text like "Press 'OK' to continue." so I cannot have to rewrite it (over and over) across all the errors I have declared in my Resource Dictionary, is that right?  I will wait for your confirmation.

Best regards,

Conrado
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ambienceCommented:
I guess you haven't read my comment - carefully enough certainly -  because I have tried to explain in detail the problems and possible solutions, including the solution to keeping your "syntax" of string definition.

In short, if you are unwilling to write any additional code, and slightly different usage, then the answer is "No"
0
 
Conrado ZAVALAAuthor Commented:
Thank you "ambience" I did read the explanation you kindly gave me, but from the standpoint of the tool I am using to code my application, I don't see any of the possible solutions something I could implement.
0
 
ambienceCommented:
what a waste of time, I should have just not commented at all.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.