控制Storyboard播放
无废话,直接上代码
XMAL代码:
<Grid Width="300" Height="460">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="60"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Grid.Triggers>
<EventTrigger SourceName="btnStart" RoutedEvent="Button.Click">
<BeginStoryboard x:Name="myImageBStoryboard" >
<Storyboard CurrentTimeInvalidated="Storyboard_CurrentTimeInvalidated">
<DoubleAnimation Storyboard.TargetName="myImageB" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="00:00:05">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger SourceName="btnPause" RoutedEvent="Button.Click">
<PauseStoryboard BeginStoryboardName="myImageBStoryboard"></PauseStoryboard>
</EventTrigger>
<EventTrigger SourceName="btnResume" RoutedEvent="Button.Click">
<ResumeStoryboard BeginStoryboardName="myImageBStoryboard"></ResumeStoryboard>
</EventTrigger>
<EventTrigger SourceName="btnStop" RoutedEvent="Button.Click">
<StopStoryboard BeginStoryboardName="myImageBStoryboard"></StopStoryboard>
</EventTrigger>
<EventTrigger SourceName="btnMiddle" RoutedEvent="Button.Click">
<SeekStoryboard BeginStoryboardName="myImageBStoryboard" Offset="00:00:02">
</SeekStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid Grid.Row="0" Grid.Column="0" >
<Image x:Name="myImageA" Source="Images/A.jpg" Height="300" Width="300" Stretch="Fill"></Image>
<Image x:Name="myImageB" Source="Images/B.jpg" Height="300" Width="300" Stretch="Fill" ></Image>
</Grid>
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<Button x:Name="btnStart" Content="Start" Width="50" Height="40" Margin="3" ></Button>
<Button x:Name="btnPause" Content="Pause" Width="50" Height="40" Margin="3" ></Button>
<Button x:Name="btnResume" Content="Resume" Width="50" Height="40" Margin="3" ></Button>
<Button x:Name="btnStop" Content="Stop" Width="50" Height="40" Margin="3" ></Button>
<Button x:Name="btnMiddle" Content="SeekToMiddle" Width="50" Height="40" Margin="3" ></Button>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Vertical">
<TextBlock x:Name="myTextBlock" FontSize="13" Foreground="Red"> </TextBlock>
<ProgressBar x:Name="myProgressBar" Height="35" Background="White" Width="300" >
<ProgressBar.Foreground>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF4DF22F" Offset="0.505" />
<GradientStop Color="#FFEFF5F1" Offset="1" />
<GradientStop Color="#FFF8F8F8" Offset="0.013" />
</LinearGradientBrush>
</ProgressBar.Foreground>
</ProgressBar>
</StackPanel>
</Grid>
C#代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Storyboard_CurrentTimeInvalidated(object sender, EventArgs e)
{
Clock storyboardClock = sender as Clock;
if (storyboardClock.CurrentProgress != null)
{
myTextBlock.Text = storyboardClock.CurrentTime.ToString();
myProgressBar.Value = (double)storyboardClock.CurrentProgress*100;(注意这儿storyboardClock.CurrentProgres值范围是0至1)
System.Diagnostics.Debug.WriteLine(myProgressBar.Value);
}
else
{
myProgressBar.Value = 0;
myTextBlock.Text = "Stopped";
}
}
}
截图:
源码:
如果山不向我走来,我就向山走去!