使用Resources.BeginInit()更新ResourceDictionary

时间:2019-01-29 19:36:21

标签: wpf resourcedictionary mergeddictionaries

我正在后面的代码中更新资源(合并字典和更改ResourceKey值)。我读过某个地方,应该在这两行Resources.BeginInit()Resources.EndInit()之间写下所有ResourceDictionary更改,以确保在完成所有更改之前不应用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 个答案:

没有答案