What's the difference between StaticResource and DynamicResource in WPF?

前端 未结 8 2022
长发绾君心
长发绾君心 2020-11-22 12:33

When using resources such as brushes, templates and styles in WPF, they can be specified either as StaticResources



        
相关标签:
8条回答
  • 2020-11-22 13:06

    A StaticResource will be resolved and assigned to the property during the loading of the XAML which occurs before the application is actually run. It will only be assigned once and any changes to resource dictionary ignored.

    A DynamicResource 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. A good example would be a forward reference to a resource defined later on in the XAML. Another example is a resource that will not even exist until runtime. It will update the target if the source resource dictionary is changed.

    0 讨论(0)
  • 2020-11-22 13:15

    Important benefit of the dynamic resources

    if application startup takes extremely long time, you must use dynamic resources, because static resources are always loaded when the window or app is created, while dynamic resources are loaded when they’re first used.

    However, you won’t see any benefit unless your resource is extremely large and complex.

    0 讨论(0)
  • 2020-11-22 13:19

    Dynamic resources can only be used when property being set is on object which is derived from dependency object or freezable where as static resources can be used anywhere. You can abstract away entire control using static resources.

    Static resources are used under following circumstances:

    1. When reaction resource changes at runtime is not required.
    2. If you need a good performance with lots of resources.
    3. While referencing resources within the same dictionary.

    Dynamic resources:

    1. Value of property or style setter theme is not known until runtime
      • This include system, aplication, theme based settings
      • This also includes forward references.
    2. Referencing large resources that may not load when page, windows, usercontrol loads.
    3. Referencing theme styles in a custom control.
    0 讨论(0)
  • 2020-11-22 13:23

    I was also confused about them. See this example below:

    <Window x:Class="WpfApplicationWPF.CommandsWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="CommandsWindow" Height="300" Width="300">
    
        <StackPanel>
            <Button Name="ButtonNew" 
                    Click="ButtonNew_Click" 
                    Background="{DynamicResource PinkBrush}">NEW</Button>
            <Image Name="ImageNew" 
                   Source="pack://application:,,,/images/winter.jpg"></Image>
        </StackPanel>
    
    
        <Window.Background>
            <DynamicResource ResourceKey="PinkBrush"></DynamicResource>
        </Window.Background>
    
    </Window>
    

    Here I have used dynamic resource for button and window and have not declared it anywhere.Upon runtime, the ResourceDictionary of the hierarchy will be checked.Since I have not defined it, I guess the default will be used.

    If I add the code below to click event of Button, since they use DynamicResource, the background will be updated accordingly.

    private void ButtonNew_Click(object sender, RoutedEventArgs e)
    {
        this.Resources.Add(  "PinkBrush"
                             ,new SolidColorBrush(SystemColors.DesktopColor)
                           );
    }
    

    If they had used StaticResource:

    • The resource has to be declared in XAML
    • And that too "before" they are used.

    Hope I cleared some confusion.

    0 讨论(0)
  • 2020-11-22 13:24

    StaticResource will be resolved on object construction.
    DynamicResource will be evaluated and resolved every time control needs the resource.

    0 讨论(0)
  • 2020-11-22 13:24

    Found all answers useful, just wanted to add one more use case.

    In a composite WPF scenario, your user control can make use of resources defined in any other parent window/control (that is going to host this user control) by referring to that resource as DynamicResource.

    As mentioned by others, Staticresource will be looked up at compile time. User controls can not refer to those resources which are defined in hosting/parent control. Though, DynamicResource could be used in this case.

    0 讨论(0)
提交回复
热议问题