WPF翻译的XAML动画到C#code [英] WPF Translating an XAML Animation to C# Code

查看:238
本文介绍了WPF翻译的XAML动画到C#code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个伟大的动画WIPE这里:的http://www.wearerighteous.com/programming/slidewipe-transition-for-your-windows-in-wpf/

I found a great WIPE animation here : http://www.wearerighteous.com/programming/slidewipe-transition-for-your-windows-in-wpf/

编辑:上面的链接是死的,所以你必须看到这一项
  <一href=\"http://learnwpf.com/post/2006/10/03/How-can-I-create-a-e2809cwipee2809d-effect-to-transition-between-two-images-in-WPF.aspx\" rel=\"nofollow\">http://learnwpf.com/post/2006/10/03/How-can-I-create-a-e2809cwipee2809d-effect-to-transition-between-two-images-in-WPF.aspx
  它具有相同的源$ C ​​$ C。

the link above is dead so you have to see this one http://learnwpf.com/post/2006/10/03/How-can-I-create-a-e2809cwipee2809d-effect-to-transition-between-two-images-in-WPF.aspx It has the same source code.

基本上,code有是这样的:

Basically, the code there is like this:

<Window.OpacityMask>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
    <GradientStop Offset="0" Color="Black" x:Name="BlackStop"/>
    <GradientStop Offset="0" Color="Transparent" x:Name="TransparentStop"/>
</LinearGradientBrush> 
<Window.OpacityMask>
    <Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="TransparentStop"
                      Storyboard.TargetProperty="Offset" By="1"  Duration="0:0:1"/>
                    <DoubleAnimation Storyboard.TargetName="BlackStop"
                      Storyboard.TargetProperty="Offset" By="1" Duration="0:0:1"
                      BeginTime="0:0:0.05" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Window.Triggers>

我试图翻译在C#code中的动画和我似乎无法做到这一点。我试了几个版本,如:

I tried to "translate" the animation in c# code and I can't seem to do it. I tried several versions, like :

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(t);
        OpacityBrush.GradientStops.Add(t2);
        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.SetTarget(DA,TransparentStop);
        Storyboard.SetTarget(DA2,BlackStop);
        Storyboard.SetTargetProperty(DA, new PropertyPath("Offset"));
        Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset"));
        sb.Begin();
    }

还是在SetTarget行替换,如:

Or replacing in the SetTarget Rows like :

Storyboard.SetTarget(DA, (ObjectToAnimate.OpacityMask as LinearGradientBrush).GradientStops[1]);

但什么也没有发生到我的FrameworkElement的。我把它叫做一个窗口(就像在从网站的例子),而出现这种情况的唯一想到的是,它设置OpacityMask。动画开始和结束的(我加了一个已完成的事件与一个MessageBox故事板只是为了看看它是否工作)。我不知道做什么其他的事情。

But nothing happens to my FrameworkElement. I called it for a Window (just like in the example from the site), and the only think that happens is that it sets the OpacityMask. The animation starts and ends (I added a Completed Event to the Storyboard with a MessageBox just to see if it works). I don't know what other things to do.

请帮帮忙! :(
任何想法?

Please help! :( Any ideas?

编辑:
也许我解释这个问题不好。我试图创建我在XAML动画,在C#code ....我一直试图两天了,寻找解决的办法。我不知道为什么C#版本无法正常工作......

Maybe I explained the question bad. I'm trying to create the animation that I have in XAML, in C# code.... I've been trying for two days now, searching for a solution. I don't know why the C# version doesn't work ...

推荐答案

我不知道为什么上面不工作,但这里的东西做的工作 - 而不是创建一个故事板,只需使用的BeginAnimation()方法每个渐变:

I'm not sure why the above is not working, but here's something that does work - instead of creating a Storyboard, simply use the BeginAnimation() method for each GradientStop:

BlackStop.BeginAnimation(GradientStop.OffsetProperty, DA2);
TransparentStop.BeginAnimation(GradientStop.OffsetProperty, DA);

这篇关于WPF翻译的XAML动画到C#code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆