Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-31
6
Medium Priority
?
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 25

Accepted Solution

by:
apeter earned 750 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 750 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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