Search code examples
wpfinternationalizationresharperresourcedictionary

How to force a XAML resource to not be deferred ( lazy loaded )


Normally XAML resources are not instantiated until they are referenced, for example

<ObjectDataProvider x:Key="T9N" ObjectType="{x:Type properties:Resources}"/>
<c:RegisterI18N x:Key="T9Nx" Source="{StaticResource T9N}"/>

The above is in a resource dictionary. It is a hack to register the ObjectDataProvider T9N with a global culture manager to provide runtime i18n.

We do

<Button Command="{Binding RestartCommand}"
        Content="{Binding restart, Source={StaticResource T9N}}" />

However unless we reference the key T9Nx somewhere in our XAML as a static resource the RegisterI18n is never instantiated.

Now I have looked through the ResourceDictionary source code.

http://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/ResourceDictionary.cs

and there are hints that there is a difference between deferred and non deferred content.

Question is: Is there a way in XAML to force an element to be immediately instantiated?

Please don't suggest alternatives to doing i18n. This is a question about deferred content loading. We have investigated all current dynamic i18n solutions and none are resharper compatible. ObjectDataProvider seems to work with resharper but we need to register every instance as it is created with a manager so we can call the Refresh() method on them on culture change. Subclassing is obvious but resharper doesn't recognise the sublcass even though the runtime behaviour is correct.


Solution

  • We now use a slightly different approach, which works pretty well by now (https://github.com/Weingartner/I18N.Reactive).
    Basically, we implemented a CustomTool (?) as an alternative to the default ResXFileCodeGenerator. The custom tool uses a T4 template to generate a class which exposes the resx keys as properties. The tool runs whenever you edit the resx file so it is updated automatically.