0
此擦除動畫只發生一次。 爲什麼不能重複?不透明度蒙版動畫
XAML
<Grid>
<MediaElement Source="" x:Name="Player1" Stretch="Fill" />
</Grid>
C#
DispatcherTimer timer = new DispatcherTimer(priority: DispatcherPriority.Background);
public MainWindow()
{
InitializeComponent();
timer.Tick += timer_Tick;
timer.Interval = TimeSpan.FromSeconds(10);
timer.Start();
Loaded += MainWindow_Loaded;
Player1.LoadedBehavior = MediaState.Manual;
Player1.Source = new Uri(@"C:\Temp\3.mov");
}
void timer_Tick(object sender, EventArgs e)
{
Player1.Stop();
Player1.Play();
WipeAnimation(Player1);
}
GradientStop BlackStop = new GradientStop(Colors.Black, 0);
GradientStop TransparentStop = new GradientStop(Colors.Transparent, 0);
public void WipeAnimation(FrameworkElement ObjectToAnimate)
{
ObjectToAnimate.OpacityMask = null;
LinearGradientBrush OpacityBrush = new LinearGradientBrush();
OpacityBrush.GradientStops.Add(BlackStop);
OpacityBrush.GradientStops.Add(TransparentStop);
ObjectToAnimate.OpacityMask = OpacityBrush;
Duration d = TimeSpan.FromSeconds(4);
Storyboard sb = new Storyboard() { Duration = d };
DoubleAnimation DA = new DoubleAnimation() { By = 1, Duration = d };
DoubleAnimation DA2 = new DoubleAnimation() { By = 1, Duration = d };
this.RegisterName("TransparentStop", TransparentStop);
this.RegisterName("BlackStop", BlackStop);
sb.Children.Clear();
sb.Children.Add(DA); sb.Children.Add(DA2);
Storyboard.SetTargetName(DA, "TransparentStop");
Storyboard.SetTargetName(DA2, "BlackStop");
Storyboard.SetTargetProperty(DA, new PropertyPath("Offset"));
Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset"));
sb.Completed += sb_Completed;
sb.Begin(this);
}
void sb_Completed(object sender, EventArgs e)
{
}
** * UPDATE * * ** * ** * ** * *
我做了WPF Image控件同樣的試驗,得到了相同的結果。動畫僅傳遞一次。
爲什麼?
public partial class MainWindow : Window
{
DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Background);
GradientStop BlackStop = new GradientStop(Colors.Black, 0);
GradientStop TransparentStop = new GradientStop(Colors.Transparent, 0);
public MainWindow()
{
InitializeComponent();
imgFace.CacheMode = new BitmapCache();
this.RegisterName("TransparentStop", TransparentStop);
this.RegisterName("BlackStop", BlackStop);
timer.Interval = TimeSpan.FromSeconds(10);
timer.Tick += timer_Tick;
timer.Start();
LinearGradientBrush OpacityBrushReset = new LinearGradientBrush();
GradientStop TransparentStopReset = new GradientStop(Colors.Transparent, 0);
OpacityBrushReset.GradientStops.Add(TransparentStopReset);
imgFace.OpacityMask = OpacityBrushReset;
}
void timer_Tick(object sender, EventArgs e)
{
WipeAnimation(imgFace);
Debug.WriteLine("A");
}
public void WipeAnimation(FrameworkElement ObjectToAnimate)
{
LinearGradientBrush OpacityBrush = new LinearGradientBrush();
OpacityBrush.StartPoint = new Point(1, 0);
OpacityBrush.EndPoint = new Point(0, 0);
OpacityBrush.GradientStops.Add(BlackStop);
OpacityBrush.GradientStops.Add(TransparentStop);
ObjectToAnimate.OpacityMask = OpacityBrush;
Duration d = TimeSpan.FromSeconds(4);
Storyboard sb = new Storyboard() { Duration = d };
DoubleAnimation DA = new DoubleAnimation() { By = 1, Duration = d };
DoubleAnimation DA2 = new DoubleAnimation() { By = 1, Duration = d };
sb.Children.Add(DA); sb.Children.Add(DA2);
Storyboard.SetTargetName(DA, "TransparentStop");
Storyboard.SetTargetName(DA2, "BlackStop");
Storyboard.SetTargetProperty(DA, new PropertyPath("Offset"));
Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset"));
sb.Completed += sb_Completed;
sb.Begin(this);
}
void sb_Completed(object sender, EventArgs e)
{
imgFace.OpacityMask = null;
}
}
<Grid>
<Image x:Name="imgFace" Source="face_avril.jpg" Stretch="Fill" HorizontalAlignment="Left" Height="176" Margin="24,34,0,0" VerticalAlignment="Top" Width="319">
<!--<Image.OpacityMask>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Offset="0" Color="Black" x:Name="BlackStop"/>
<GradientStop Offset="0" Color="Transparent" x:Name="TransparentStop"/>
</LinearGradientBrush>
</Image.OpacityMask>-->
</Image>
</Grid>
1 0000000謝謝你,哥哥!!!! –