dotnet / dotnet/wpf

Creating resource dictionary through code behind lead to performance delay

Open
#6,652 6 comments 0 reactions 0 assignees View on GitHub
Performance
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

**Problem 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](https://github.com/dotnet/wpf/files/8844952/ResourceDictionary-Performance-issue.zip)

Replication procedure:
1. Run the demo.
2. Click button to merge 100 resource dictionaries to current application.
3. 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.