What is the difference between Static and Dynamic resources?

Open 1 Answers 46 Views Education
What is the difference between Static and Dynamic resources?

1 Answer

The most basic difference is that StaticResource evaluates the resource one time only, but DynamicResource evaluates it every time the resource is required. And due to this reason, DyanamicResource is heavy on the system but it makes pages or windows load faster.
Static Resources
A Static Resource will be resolved and assigned to the property during the loading of the XAML that occurs before the application is actually run. It will only be assigned once and any changes to the resource dictionary is ignored.
Static resource references work best for the following circumstances:
  • Your application design concentrates most of its resources into page or application level resource dictionaries. Static resource references are not re-evaluated based on runtime behaviours such as reloading a page, and therefore there can be some performance benefit to avoiding large numbers of dynamic resource references when they are not necessary per your resource and application design.
  • You are setting the value of a property that is not on a Dependency Object or a freezable.
  • You are creating a resource dictionary that will be compiled into a DLL, and packaged as part of the application or shared between applications.
Dynamic Resources
A Dynamic Resource assigns an Expression object to the property during loading but does not actually lookup the resource until runtime when the Expression object is asked for the value. This defers looking up the resource until it is needed at runtime.
Dynamic resources work best for the following circumstances:
  • The value of the resource depends on conditions that are not known until runtime. This includes system resources, or resources that are otherwise user settable. For example, you can create setter values that refer to system properties, as exposed by System Colours, System Fonts, or System Parameters. These values are truly dynamic because they ultimately come from the runtime environment of the user and operating system. You might also have application-level themes that can change, where page-level resource access must also capture the change.
  • You are creating or referencing theme styles for a custom control.
  • You intend to adjust the contents of a Resource Dictionary during an application lifetime.
  1. <Window.Resources>  
  2.     <SolidColorBrush x:Key="brush" Color="Green" />  
  3.     <Style TargetType="Border" x:Key="PageBackground">  
  4. <Setter Property="Background" Value="Gold"/>  
  5.     </Style>  
  6. </Window.Resources>  
  7. <Grid>  
  8.     <Border Style="{DynamicResource PageBackground}">  
  9.         <Button x:Name="btn" Content="Rajkumar Test" Click="Button_Click" Background="{DynamicResource brush}" Height="30" Margin="53,130,85,130" /> </Border>  
  10. </Grid>  

You can apply on code behind like the following,

  1. private void Button_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    this.btn.SetResourceReference(BackgroundProperty, "brush");  
  4. }  
by (279k points)