You can write an ApplicationDictionaryMerger class that takes dictionaries as its contents and adds them to the applicationโs MergedDictionaries , for example:
[ContentProperty("Dictionaries")] public class ApplicationDictionaryMerger { private readonly ObservableCollection<ResourceDictionary> dictionaries = new ObservableCollection<ResourceDictionary>(); public ApplicationDictionaryMerger() { this.dictionaries.CollectionChanged += this.DictionariesChanged; } private void DictionariesChanged(object sender, NotifyCollectionChangedEventArgs e) {
The only thing you need to do is that you need to instantiate an object like the one above from XAML.
Initially, I thought that adding it to the Resources section of any control in your XAML would be fine, but then it turns out that the XAML loader does not create resources that are not used. So I came up with another workaround: setting the object as the value of any Tag control.
I would be very interested to know if there is a better way to ensure that ApplicationDictionaryMerger .
Here's how to use it:
<Grid> <Grid.Tag> <sandbox:ApplicationDictionaryMerger> <ResourceDictionary> </ResourceDictionary> </sandbox:ApplicationDictionaryMerger> </Grid.Tag> </Grid>
source share