首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LinearGradientBrush动画

LinearGradientBrush动画
EN

Stack Overflow用户
提问于 2012-12-10 21:48:13
回答 1查看 1.7K关注 0票数 0

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

所以我喜欢:

代码语言:javascript
复制
 <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>

代码语言:javascript
复制
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");
        }

但是什么都没发生。:(

有什么线索没有了吗?

谢谢!

附言:我在这里发现同样的问题已经太晚了,WPF Translating an XAML Animation to C# Code

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-10 23:30:19

所以我在这里找到的正确代码是WPF Translating an XAML Animation to C# Code

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

代码语言:javascript
复制
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!:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13802253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档