Solved

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

Posted on 2015-01-31
6
129 Views
Last Modified: 2015-02-04
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
0
Comment
Question by:Conrado ZAVALA
  • 3
  • 2
6 Comments
 
LVL 25

Accepted Solution

by:
apeter earned 250 total points
ID: 40582625
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
 
LVL 22

Assisted Solution

by:ambience
ambience earned 250 total points
ID: 40583634
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
 

Author Comment

by:Conrado ZAVALA
ID: 40584401
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 22

Expert Comment

by:ambience
ID: 40584644
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
 

Author Comment

by:Conrado ZAVALA
ID: 40587589
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
 
LVL 22

Expert Comment

by:ambience
ID: 40588182
what a waste of time, I should have just not commented at all.
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question