'Relation between resource referencing way and sharing regarding Storyboard
<!-- Here x:Shared is modified -->
<SolidColorBrush x:Key="SolidBrush" x:Shared="False" Color="White"/>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid x:Name="Root">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" >
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="Background.Color">
<EasingColorKeyFrame KeyTime="0" Value="Red" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="Background.Color">
<EasingColorKeyFrame KeyTime="0" Value="Blue" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<!-- Here I change the refencing way of Background -->
<Border x:Name="Border" Background="{DynamicResource SolidBrush}">
<ContentPresenter x:Name="ContentSite" ContentSource="Header" RecognizesAccessKey="True" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I wondered how Storyboard works regarding resource referencing way and sharing. So I tested the above code and got a result like below.
Reference way in Border |
x:Shared |
Result |
|---|---|---|
DynamicResource |
True |
IsFrozen==True |
DynamicResource |
False |
Cannot Animate Background.Color on an Immutable Object Instance Error |
StaticResource |
True |
IsFrozen==False |
StaticResource |
False |
IsFrozen==False |
Debug.WriteLine((Resources["SolidBrush"] as SolidColorBrush).IsFrozen);
IsFrozen is a result of above code.
I found that if resource is referred in a Style it is automatically frozen.(SO, I don't know why frozen). As storyboard clone a frozen resource to animate it, I can understand why DynamicResource works correctly.
But I cannot understand why a resource is not frozen in case of StaticResource, and why referencing non-shareable resource causes Cannot Animate an Immutable Object Instance error.
Why these happen?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
