Reputation: 1779
I want to make blinking TextBlock's Text. But, Storyboard is not accessed. Please review code as below:
XAML
<Page.Resources>
<Storyboard x:Name="BlinkLabelStoryBoard" x:Key="BlinkLabel" Duration="0:0:2" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames
Storyboard.TargetName="DeviceState"
Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)">
<DiscreteColorKeyFrame KeyTime="0:0:0" Value="White"/>
<DiscreteColorKeyFrame KeyTime="0:0:1" Value="OrangeRed"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Page.Resources>
<TextBlock x:Name="DeviceState" Text="{Binding RunMode}" FontWeight="Bold" FontSize="16" HorizontalAlignment="Center" Loaded="Start_Animation">
Behind Code
private void Start_Animation(object sender, RoutedEventArgs e)
{
Storyboard board = (FindResource("BlinkLabelStoryBoard") as Storyboard);
board.Begin();
}
But, An error happens BlinkLabelStoryBoard resource not found
.
And, Another error happens DeviceState
resource not found.
Upvotes: 0
Views: 256
Reputation: 1779
There was some mistakes in my code.
(Thanks for Emo de Weerd) FindResource was not able to find BlinkLabelStoryBoard
because FindResource expects a Key not a Name. So, I changed a code in Start_Animation handler and Deleted a Storyboard's Name in Xaml.
Storyboard board = (FindResource("BlinkLabel") as Storyboard);
Storyboard.TargetName was not able to find the name of Target TextBlock. So, I changed Storyboard.TargetName to {Binding ElementName=DeviceState}
.
board.Begin required a target element. So, I changed a related code to board.Begin(sender as TextBlock)
Finally, Please refer full code as below:
XAML
<Page.Resources>
<Storyboard x:Key="BlinkLabel" Duration="0:0:2" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames
Storyboard.TargetName="{Binding ElementName=DeviceState}"
Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)">
<DiscreteColorKeyFrame KeyTime="0:0:0" Value="White"/>
<DiscreteColorKeyFrame KeyTime="0:0:1" Value="OrangeRed"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Page.Resources>
...
<TextBlock x:Name="DeviceState" Text="{Binding RunMode}" FontWeight="Bold" FontSize="16" HorizontalAlignment="Center" Loaded="Start_Animation">
Behind Code
private void Start_Animation(object sender, RoutedEventArgs e)
{
Storyboard board = (FindResource("BlinkLabel") as Storyboard);
board.Begin(sender as TextBlock);
}
Upvotes: 0
Reputation: 50672
FindResource expects a Key not a Name.
XAML
<Page.Resources>
<Storyboard x:Key="BlinkLabel" Duration="0:0:2" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames
C#
private void Start_Animation(object sender, RoutedEventArgs e)
{
Storyboard board = (FindResource("BlinkLabel") as Storyboard);
A Name turns into an identifier in the C# code. A Key is an index in a dictionary, a ResourceDictionary in this case.
Upvotes: 1