Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Resource Dictionary efficiently in all parts of project

I am using resource dictionary(same dictionary) in many converters as a local variable.

var DeignerDictionary = new ResourceDictionary
{
  Source = new Uri(path)
};

Each time I am creates a new instance and the memory goes very high in the application.

Then I have moved the resource dictionary into a static field and I am reusing the dictionary , but the styles are not rendered properly.

public class resourceDictionaryProvider{
    public readonly ResourceDictionary StaticDictionary =
    new ResourceDictionar {Source = new Uri(path)};
}

Can anyone suggest what I am doing wrong,Please provide your suggestions.

The issue occurs after changed the ResourceDictionary as static only.But the following code works fine.

public class resourceDictionaryProvider{
    public static readonly ResourceDictionary StaticDictionary =
    new ResourceDictionar {Source = new Uri(path)};
}

Now I am creating an instance for resourceDictionaryProvider class and it works fine,But I don't want to create instances So only I have changed it to static.

What is the problem with the static keyword here?

like image 334
Venkat Avatar asked Nov 14 '16 12:11

Venkat


2 Answers

This is a well known problem with WPF ResourceDictionaries. The solution would be to implement a SharedResourceDictionary construct on your own which prevents re-instantiation of the resources on each use. Take a look at this link : WPF SharedResourceDictionary for an awesome implementation of the SharedResourceDictionary construct.( All credits to the author )

like image 78
jester Avatar answered Nov 03 '22 06:11

jester


There are two issues you need to address:

  1. The resource dictionaries can be shared across the modules of your project or solution;
  2. It would be handy to have design time resource dictionary to assist with the styling settings.

To solve the issue #1, it's easy to add your resource dictionaries to the App.xaml file, and then they will be instantiated once, and will be available to the entire project, like this:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary
                 Source="pack://application:,,,/Themes;component/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

To solve the issue #2, you need a workaround solution that the resource dictionary is instantiated at the design time only. Check out Design time resource dictionary

and then, you can use the DesignTimeResourceDictionary on your UIs, like this:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <designer:DesignTimeResourceDictionary
                 Source="pack://application:,,,/Themes;component/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
like image 42
Lin Song Yang Avatar answered Nov 03 '22 08:11

Lin Song Yang