WPF performance impact ResourceDictionary merging vs updating

被刻印的时光 ゝ 提交于 2019-12-11 16:16:13

问题


I have to update font size dynamically in WPF app. Performance wise which is a better approach or what are pros & cons of below 2 approaches?

  1. Merging dictionary [see API OnFontSizeChanged_MergeDictionary]
  2. Updating Resource Key [see API OnFontSizeChanged_UpdateResourceKey]

I have defined all used FontSizes in GenericResourceDictionary.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:System="clr-namespace:System;assembly=mscorlib">

    <System:Double x:Key="FontSize_Small">12</System:Double>
    <System:Double x:Key="FontSize_Default">14</System:Double>
    <System:Double x:Key="FontSize_DefaultPlus">16</System:Double>
    <System:Double x:Key="FontSize_HeaderNormal">18</System:Double>
    <System:Double x:Key="FontSize_HeaderLarge">20</System:Double>
    <System:Double x:Key="FontSize_MenuItem">26</System:Double>

</ResourceDictionary>

User initiated runtime FonstSize change in ViewModel:

Approach#1: Create a new ResourceDictionary and Merge it. Remove the old font size dictionary (I do not remove GenericResourceDictionary.xmal as it has far more styles than just Fonts)

    private void OnFontSizeChanged_MergeDictionary(string fontSize)
            {

                string currentDefaultFontSize = Application.Current.Resources["FontSize_Default"]!=null ? Application.Current.Resources["FontSize_Default"].ToString() : null;

                //If Current default fontsize is equal to what user is trying to set, then do nothing
                if ((currentDefaultFontSize == fontSize) || fontSize.IsNullOrEmpty())
                    return;

                double newDefaultFontSize = Convert.ToDouble(fontSize);

                ResourceDictionary rd = new ResourceDictionary();
                rd.Add("FontSize_Small", newDefaultFontSize - 2);
                rd.Add("FontSize_Default", newDefaultFontSize);
                rd.Add("FontSize_DefaultPlus", newDefaultFontSize + 2);
                rd.Add("FontSize_HeaderNormal", newDefaultFontSize + 4);
                rd.Add("FontSize_HeaderLarge", newDefaultFontSize + 6);
                rd.Add("FontSize_MenuItem", newDefaultFontSize + 12);

               //Unload old fontSizeResource
Application.Current.Resources.MergedDictionaries.Remove(_fontSizeRD);

              //Load new fontSizeResource
Application.Current.Resources.MergedDictionaries.Add(resource);

             //assign the new resource to class variable
_fontSizeRD=resource;
            }

Approach#2: Updated Resource key in GenericResourceDictionary.xaml

private void OnFontSizeChanged_UpdateResourceKey(string fontSize)
            {

                string currentDefaultFontSize = Application.Current.Resources["FontSize_Default"]!=null ? Application.Current.Resources["FontSize_Default"].ToString() : null;

                //If Current default fontsize is equal to what user is trying to set, then do nothing
                if ((currentDefaultFontSize == fontSize) || fontSize.IsNullOrEmpty())
                    return;

                double newDefaultFontSize = Convert.ToDouble(fontSize);

                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;
            }

Thanks,

RDV

来源:https://stackoverflow.com/questions/54428306/wpf-performance-impact-resourcedictionary-merging-vs-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!