Solved

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

Posted on 2015-01-31
6
119 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now