Creating resource dictionary through code behind lead to performance delay
See original GitHub issueProblem description:
My application contains more than 50+ MS and 150 + custom control, each control style have been defined in separate resource dictionary file and merged MS control resource dictionary in custom control resource dictionary file. And when I have merge control resource dictionary into application resources property through code behind. I am facing serve performance delay in creating resource dictionary at code behind.
`private void Button_Click(object sender, RoutedEventArgs e) { stopwatch.Start(); for (int i=0; i <= 100; i++) { Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(“/ResourceDictionary-Performance-issue;component/MSControl/CustomControl.xaml”, UriKind.RelativeOrAbsolute) }); }
stopwatch.Stop();
MessageBox.Show("Time delay in adding resource:" + stopwatch.Elapsed.TotalSeconds);
} ` Also, I have tired to reproduce in simple application. Please find the demo application attached below:
Sample: ResourceDictionary-Performance-issue.zip
Replication procedure:
- Run the demo.
- Click button to merge 100 resource dictionaries to current application.
- It took more than 4.5 sec.
Actual behavior: Performance delay occurs when creating resource dictionary.
Expected behavior: Performance delay should not occurs when creating resource dictionary.
Can anyone suggest a solution regarding this how to avoid performance delay when creating resource dictionary at code behind.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Having a lot of resource dictionaries is expensive and probably the reason why WPF merges all it’s resources into one generic.xaml during compile. So i suggest you do the same. Feel free to use the tool/msbuild task i built for that https://github.com/batzen/XAMLTools#xamlcombine
@SubashiniMahendran with my solution you can keep your files separate for development and use the combined file for production.