using {DynamicResource {x:Static LocalizableTexts.OpenTextKey}}
-
05-07-2019 - |
Question
I have been trying to come up with a better localization approach than LocBaml (wich sucks). And I though I could use something like {DynamicResource {x:Static LocalizableTexts.OpenTextKey}} (dynamicresource because my language could change at runtime). The only question is? How do I correctly create LocalizableTexts.OpenTextKey ? Is there a tutorial out there that can teach me how to create custom RessourceKeys and how to update them at runtime?
I am living in the MVVM world by the way ...
Solution
I think your after component resource keys.
Start by defining your resource in a XAML resource dictionary, something like:
<local:OpenText x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:LocalizableTexts}, ResourceId=OpenText}" />
Then create a static accessor class:
public static class LocalizableTexts
{
public static ComponentResourceKey OpenTextKey
{
get { return new ComponentResourceKey(typeof(LocalizableTexts), "OpenText"); }
}
}
Finally be sure you indicate the xml namespace when you use a static binding:
{DynamicResource {x:Static local:LocalizableTexts.OpenTextKey}}