in Education by
What is the difference between Static and Dynamic resources?

1 Answer

0 votes
by
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.
Example
  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. }  

Related questions

0 votes
0 votes
0 votes
    What is ToolTip and how we use it in WPF?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    What is Attached Properties and how to register it?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    What is WPF Dependency Property and how can we use?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    What are various ways of doing alignment in WPF?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    What is a WPF Child Window?...
asked Apr 9, 2021 in Education by JackTerrance
...