I am using silver light 4 and for XAML text blocks binding i want to use multiple resource file but unable to implement it. one link is supporting the concept of not having multiple resource file of same language at runtime dynamically
http://msdn.microsoft.com/en-us/magazine/gg650657.aspx[^]
i m using the following code for binding a text block using 1 resource file
where ResourceKey is the key defined at App.xaml and CNICNumberTextBlock is the name defined in resource file
App.xaml key defining:
Kindly help me to make use of multiple resource file at run time
I have came across some links which might be useful to u..
Localisation - Michael Snow
Jeff Prosise Blog
Related
I have a .xaml UserControl named MyUserControl.xaml and I want to set its resource URI.
Per default, WPF generates a URI that includes a resource name, which is equal to the resource it belongs to such as
"/MyNamespace;component/myusercontrol.xaml"
for the .xaml named MyUserControl.xaml
How can I have a UserControl MyUserControl.xaml and make WPF generate an individual resource identifies such as
"/MyNamespace;component/myusercontrol_A.xaml" or
"/MyNamespace;component/myusercontrol_B.xaml" ?
The reason why I want to do that is described here.
In the image below you can see the resource identifier I am talking about:
and therein:
Note, that that question is the origin of this question and might help to understand its background.
After a week suffering and laboring with this issue, I finally found both the reason for the problem and its solution.
The problem lies within the auto-generated *.g.i.cs file, which is called by the InitializeComponent() method of a UserControl, as seen by the following:
This file generates a string (a Resource Locator) that expresses the path to that xaml-component, as seen by the following:
Now, if you have multiple versions of the same assembly and both versions include the same xaml-file, WPF does not know what xaml-file to instantiate, because the Resource Locator only references the name of the assembly but not its version.
This results in a TargetInvocationException, saying that
{"The component 'MyNamespace.MyUserControl' does not have a resource identified by the URI '/MyAssembly;comoponent/myusercontrol.xaml'"}
as follows:
The simple (but most definitely not obvious) solution for this is to add the version of the assembly to this Resource Locator. This can be achieved by modifying the build-file of the project by adding the <AssemblyVersion>-tag as follows:
Credits for this go to:
this blog
this SO thread
Currently I add an image in XAML this way :
Put the file in the project's folder, in a sub-directory named "Resources",
Set its "Build Action" property to "Resource",
Add in my XAML file : <Image Source="/Resources/myImage.png />
And it works great.
But in SO I keep seeing people writing this instead :
<Image Source="pack://application:,,,/MyApplicationNamespace;component/Resources/myImage.jpg" />
Also recently, I've found that in Project Properties -> Resources, you can add files like images, texts...
So which of these 3 possibilities should I use ?
The Image Source="/Resources/myImage.png syntax used in your XAML to refer to an image in a sub folder is actually equivalent to the following syntax pack://application:,,,/Resources/myImage.jpg and this is one of the variations used to access binary resources in XAML.
Since the first two options are the same written differently this leaves us with the third option.
When to use the resource file ?
I usually tend to use them when i have different resources assemblies that are used as satellite assemblies which are in turn used for localization to different cultures. Also they can be used when you want to access those resources and switch them at run-time.
I have an composite application which has a Shell window and has some modules, each module is loaded on to the shell using MEF (Microsfot Prism). Shell Itself has a resource file default(Resource.resx) and for other languages(like Resource.ar-SA.resx) too to support Localilzation.
For Xaml : I had used x:Static Resource.KeyName
In c# Code : I had used GetString(keyName, culture) to get the required string.
To support Localization, I have added a line of code which gets the current culture of the system and loads the required Resx File.
For Setting the Current Culture.
Resource.Culture =
new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name);
in App.Xaml.cs
All these work Completely fine.
Similarly, each module which is loaded using MEF has Resource files as mentioned above. But in case of Modules, resource Key defined in the Xaml for a specific language file is not working. Its always getting the values from the Default (Resource.Resx) file. But any key used in the code using ResourceManager class works fine.
I ended up keeping all my resources in an "Infrastructure" project so all the resources are loaded from one place, it just made it simpler.
My xaml bindings look like this
Get the refrence to your resx class.
xmlns:resources="clr-namespace:Infrastructure.resources;assembly=Infrastructure"
now get the text
Content="{x:Static resources:Resources.Activity_Regarding}"
You might be doing this already but I cant see. You would want to make sure the namespace matches for the resx you want to use. So your modules namespace.
I'm attempting to use a resx file to localize some strings I am using in a XAML file. I've looked around at other documentation on the web, and they all seem to recommend a two part process:
Add a clr-namespace to your window, like this:
xmlns:props="clr-namespace:PJConfiguration.Properties"
Use that namespace to localize your string like this:
Content="{x:Static props:Resources.SharedSettings}"
I've done this, and also made sure that my resource classes are public, but I still get the following error from the XAML in step 2:
Cannot find the type 'Resources'.
Does anyone know what else might be causing this problem? Thanks in advance.
In order to make the Resources visible to XAML, you have to make sure that the code generation mode for the resources is set to public. In VS, you find that setting in a ComboBox near the top of the Resources designer window.
For more information on using .Net resources in XAML, you might want to refer to these blog posts: http://wpfglue.wordpress.com/category/localization/
Check if your .resx file is the default Resources.resx file inside the Properties directory of the Application assembly. If that is, there is no reason XAML couldn't find the public class Resources from the correct namespace under local assembly.
Try to specify the assembly name in Step 2 as recommended in this answer.
In a WPF application, for a localizable text resource such as an error message, should I be using a .resx file or a ResourceDictionary. If the answer is either/or what factors would help me decide which to use?
Using .resx files - with the name of the culture in the filename - is probably the easiest way to go. The loading of the appropriate resources is handled for you.
So you'd have "ResourceFile.resx" as your default and then "ResourceFile.en-GB.resx", "ResourceFile.fr-FR.resx" etc for your localised strings.
You only need to put those strings that actually need localisation in the language files. If the string's not present in the culture specific file it falls back to the default file.