Saturday, 22 August 2015

Resources and types of Resources in WPF


What are the Types of Resources in WPF?


1) Binary Resources
Binary resources could be logo/image files, AV files etc.

2) Logical Resources are of tow types:
Static and Dynamic Resources

* StaticResource finds the key defined within the ResourceDictionary under its scope during the Loading of the application.
* Hence the Compiler will throw error during compilation if not found in resources.
--
* DynamicResource Markup Extension defers the resource assignment to the actual runtime of the application.
* So the expression remains unevaluated until the object being created.

What are Dynamic Resources in WPF?


* DynamicResource Markup Extension defers the resource assignment to the actual runtime of the application.
* So the expression remains unevaluated until the object being created.

What is the Choice between StaticResource and DynamicResource in WPF?


* StaticResource requires less CPU during runtime, so it is faster.
* StaticResource are created when the application loads. So making everything as StaticResource means slowing down the Application Load process.
* When the resources are unknown during compilation, you can use DynamicResource.
* DynamicResource are used when user interaction changes the look and feel of an object.
What is the main difference. Like memory or performance implications
The difference between static and dynamic resources comes when the underlying object changes. If your Brush defined in the Resources collection were accessed in code and set to a different object instance, Rectangle will not detect this change.
Static Resources retrieved once by referencing element and used for the lifetime of the resources. Whereas, DynamicResources retrieve every time they are used.
The downside of Dynamic resources is that they tend to decrease application performance.
Are there rules in WPF like "brushes are always static" and "templates are always dynamic" etc.?
The best practice is to use Static Resources unless there is a specific reason like you want to change resource in the code behind dynamically. Another example of instance in which you would want t to use dynamic resoruces include when you use the SystemBrushes, SystenFonts and System Parameters.
Resources can be referred statically or dynamically. Static referred resources evaluate the resource only once and after that if the resources change those changes are not reflected in the binding. While dynamic referred resources are evaluated every time the resource is needed.
Consider the below “SolidColorBrush” resource which is set to “LightBlue” color.
<Window.Resources>
<SolidColorBrush Color="LightBlue" x:Key="buttonBackground" />
</Window.Resources>
The above resource is binded with the below two textboxes statically and dynamically respectively.
<TextBox Background="{DynamicResource buttonBackground}" />
<textbox background="{StaticResource buttonBackground}">
Now if we modify the resource , you see the first text box changes the background and the other textbox color stays as it is.
private void Button_Click(object sender, RoutedEventArgs e)
{
          Resources["buttonBackground"] = Brushes.Black;
}
Below is the output of the same.

No comments:

Post a Comment