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)
    {
    }

** *更新* * ** * ** * ** * *

我用 WPF Image 控件做了同样的测试,得到了同样的结果。动画仅通过 1 次。

为什么?

  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>
4

1 回答 1

1

你有没有尝试过:

DoubleAnimation DA = new DoubleAnimation() { From = 0, To = 1, Duration = d };
DoubleAnimation DA2 = new DoubleAnimation() { From = 0, To = 1, Duration = d };

这样,Offset在动画再次开始之前,属性将被重置为其原始值

于 2012-12-11T21:43:14.767 回答