How to access form resources in runtime in Windows Forms? - winforms

I need to extract some resources from the MyForm.resx file in runtime. In particular, I need to access captions of controls, which are stored inside.
From what I know, resources are embedded into assembly during compilation. Knowing specific form I want access to (let it be MyForm) and specific name of the resource (like lbSth.Text), is there a way to access those resources in runtime?

There are two ways, depending on what is actually needed.
One may use System.Resources.ResourceManager class. Construct it by passing a Type of form or control, which resources you want to access. It automatically resolves satellite assemblies and provides access to resources related to a form. The downside is that it doesn't allow iterating through key/value pairs, only accessing a resource of known key, so you have to know them in advance (for instance, process resx files beforehand)
It turns out, that resources in binary form are kept as embedded resources in the assembly. However, translations are being kept in satellite assemblies. To access those, use eg. Assembly.GetExecutingAssembly().GetSatelliteAssembly(), however keep in mind, that resources in there will have different names. For instance, MyForm.resources in satellite assembly will be named, eg. MyForm.en.resources.
Afterwards, use resolvedAssembly.GetManifestResourceStream() to access embedded resource and then finally System.Resources.ResourceReader class to iterate through resources in that file.

Related

Prevent WPFLocalizeExtension from trying to load resources for all possible languages

I am just beginning to use the WPFLocalizeExtension in a project. It works, but it has a serious impact on the startup performance of the app. It tries to load the resources for all possible languages, including many which we won't ever provide resources for. Normally that might happen without notice, but in this case we have a special folder structure for some of the loaded assemblies. Although the resource DLLs are still situated in language folders directly beneath the folder of the executable, but the app gets an AssemblyResolve event for every language.
Thanks for your help in advance.
We had the same problem and approached it by modifying the WPFLocalizeExtension source code.
When you have a look at the code, you will find a class called ResxLocalizationProviderBase. This class includes a method GetResourcemanager, which causes the massive amounts of AssemblyResolve events, because it tries to get the resource set of all cultures that ship with the .NET Framework (line 498):
var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (var c in cultures)
{
...
}
We modified the code by creating a list of CultureInfo objects, that includes only languages, we'd like to provide resources for.

Loading a new Silverlight application from isolated storage

Is there any way i can load a silverlight application from isolated storage and replace the current application ?
Short answer: no, you cannot do this. You could however store a dll, read and load it via reflection if you are in a trusted out-of-the-browser application.
Anyway I don't believe this to be a very good practice, it's very likely there is a different better way to solve your particular problem.
You can't replace the currently running application from within Silverlight code.
However you could create a single application to act as a shell. You could store assemblies or entire Xaps in isolated storage. Using the AssemblyPart class allows you to load an assembly from this storage, create an instance of an entry type and execute an entry point method.
The big caveat would be that this "stored application" would have to be coded specifically to work within your shell. You would need to provide your ways in particular to allow for "application" level state to be accessed and a means of loading content assets such as images.

Accessing external static resources placed in different assembly and XAP

My prism based silveright application is divided into multiple xaps to reduce initial download size and support multi tenant application.
However, I cannot access the resource dictionaries defined in external assembly (i.e. the resource .xaml file is part of an assembly that complies/ is copied into a different xap)
Any suggestions ? because the standard syntax for accessing external resources files does not work
The book Pro Silverlight 2 in VB 2008 (p166) [MacDonald] says "Unfortunately, Silverlight doesn't currently allow you to merge resource dictionaries, which means there is no way to split your application resources into separate files and then merge them into your application (which is possible in WPF)." I don't know if things have changed since then, but I would suggest that is the reason.

Change resource file on the fly

I woulld like to localize my WPF application with resource files. It good technics. But I have requirement to give ability to end user to change some localization information (for example some word traslation). It means change information in the resourse files on the fly (in run time). Is it possible ?
This would involve recompiling the resources on the fly; and reloading them will be quite difficult (as DLLs cannot be unloaded without unloading an AppDomain).
In such a configuration, you're better off using the database to store your translations.

how to include XSD schema files in Silverlight library?

Within a Silverlight library, I need to validate incoming XML against a schema. The schema is composed of 5 interdependent .xsd files; the main file uses "xs:import" to reference all of the others, and there are other references among them.
Assuming that the .xsd files need to be distributed with the library (i.e. not hosted on some well-known external URL), how should I structure my project to include them?
I have found that I can embed them in the library project with build type "Resource" and then load them (individually) using Application.GetResourceStream() and a relative URI with the ";content" flag in it. But if I take this approach, can I validate against the interdependent set of 5 files? What happens when the schema parser tries to resolve the interdependencies?
Or should I embed them with build type "Content" and access the main one with some other sort of URL?
Or???
To summarize: how should I use these 5 .xsd files in my project so that I will be able to validate XML against them?
EDIT: It's not clear whether it's even possible to validate in Silverlight. I spun off a related question.
I cannot say much about Silverlight limitations with respect to validation, but the question itself is more generic - one might want to store .xsd files as resources in a desktop .NET application, for example - so I will answer that part.
You can have full control over resolution of URIs in xs:import by means of XmlSchemaSet.XmlResolver property. Just create your own subclass of XmlResolver, override GetEntity() method, and implement it using GetResourceStream(), or GetManifestResourceStream(), or whichever other way you prefer.

Resources