Обновление ResourceDictionary с помощью Resources.BeginInit()

Я обновляю ресурсы (объединяя словари и меняя значения ResourceKey) в коде позади. Я где-то читал, что я должен написать все изменения ResourceDictionary между этими строками Resources.BeginInit() а также Resources.EndInit() что способы DynamicResources не применяются, пока все изменения не будут сделаны.

Кто-нибудь может дать больше разъяснений о том, что происходит, когда ресурс добавляется / удаляется из объединенных словарей, когда обновляется ключ ресурса?

Пример:

    private void _replaceResourceDictionary(ResourceDictionary newResource, ResourceDictionary oldResource)
    {
         double newDefaultFontSize = 15.0;

        //This info may be incorrect: Using Begin & End ensures that DynamicResources are not refresh multiple times
        Application.Current.Resources.BeginInit();
        _removeFromMergedDictionaries(oldResource);
        _addToMergedDictionaries(newResource);
        Application.Current.Resources.EndInit();

       //Question: Does using Begin & End will benefit here as well?
        Application.Current.Resources.BeginInit();
        Application.Current.Resources["FontSize_Small"] = newDefaultFontSize - 2;
        Application.Current.Resources["FontSize_Default"] = newDefaultFontSize;
        Application.Current.Resources["FontSize_DefaultPlus"] = newDefaultFontSize + 2;
        Application.Current.Resources["FontSize_HeaderNormal"] = newDefaultFontSize + 4;
        Application.Current.Resources["FontSize_HeaderLarge"] = newDefaultFontSize + 6;
        Application.Current.Resources["FontSize_MenuItem"] = newDefaultFontSize + 12;
        Application.Current.Resources.EndInit();
    }

Спасибо,

RDV

0 ответов

Другие вопросы по тегам