LinearGradientBrush动画

时间:2012-12-10 13:48:13

标签: c# .net wpf xaml

我正在尝试转换“擦除”动画http://learnwpf.com/post/2006/10/03/How-can-I-create-a-e2809cwipee2809d-effect-to-transition-between-two-images-in-WPF.aspx示例以在C#中使用它。

所以我喜欢:

 <Grid>
        <Image Source="C:\Temp\WMS\Others\megan_fox_17-normal.jpg" />
        <Image Source="C:\Temp\WMS\Others\Hudgens.jpg">
            <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>
        <Button Content="Button" HorizontalAlignment="Left" Margin="72,288,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    </Grid>

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var _doubleAnimationFrontPlayer = new DoubleAnimation();
            _doubleAnimationFrontPlayer.By = 1;
            _doubleAnimationFrontPlayer.Duration = new Duration(TimeSpan.FromSeconds(2));

            sb.Children.Add(_doubleAnimationFrontPlayer);
            Storyboard.SetTargetProperty(_doubleAnimationFrontPlayer, new PropertyPath(GradientStop.OffsetProperty));
            Storyboard.SetTargetName(TransparentStop, "TransparentStop");
            Storyboard.SetTarget(_doubleAnimationFrontPlayer, TransparentStop);

            var _doubleAnimationFrontPlayer2 = new DoubleAnimation();
            _doubleAnimationFrontPlayer2.By = 1;
            _doubleAnimationFrontPlayer2.Duration = new Duration(TimeSpan.FromSeconds(2));
            _doubleAnimationFrontPlayer2.BeginTime =  TimeSpan.FromMilliseconds(1000) ;

            sb.Children.Add(_doubleAnimationFrontPlayer2);
            Storyboard.SetTargetProperty(_doubleAnimationFrontPlayer2, new PropertyPath(GradientStop.OffsetProperty));
           Storyboard.SetTargetName(BlackStop, "BlackStop");
            Storyboard.SetTarget(_doubleAnimationFrontPlayer2, BlackStop);

            sb.Completed += sb_Completed;
            sb.Begin();            
        }

        void sb_Completed(object sender, EventArgs e)
        {
            Debug.WriteLine("DONE");
        }

但没有任何反应...... :(

有什么缺失的线索?

谢谢!

P.S。我在WPF Translating an XAML Animation to C# Code

找到了同样的问题

1 个答案:

答案 0 :(得分:0)

我在这里找到了错误的代码WPF Translating an XAML Animation to C# Code

我刚刚编辑了它,因为我发现了3个错误:

public void WipeAnimation(FrameworkElement ObjectToAnimate)
{
    LinearGradientBrush OpacityBrush = new LinearGradientBrush();
    OpacityBrush.StartPoint = new Point(1, 0);
    OpacityBrush.EndPoint = new Point(0, 0);
    GradientStop BlackStop = new GradientStop(Colors.Black, 0);
    GradientStop TransparentStop = new GradientStop(Colors.Transparent, 0);
    OpacityBrush.GradientStops.Add(BlackStop);
    OpacityBrush.GradientStops.Add(TransparentStop);
    ObjectToAnimate.OpacityMask = OpacityBrush;

    this.RegisterName("TransparentStop", TransparentStop);
    this.RegisterName("BlackStop", BlackStop);

    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.Begin(this);
}

作者也获得了+1! :)