WP7 - 通过代码淡化用户控制,而不是XAML

时间:2011-05-18 19:30:40

标签: c# wpf windows-phone-7

我正在尝试弹出一个用户控件,然后在3秒内将其淡出。我正在尝试使用以下代码,但我在Popup.LoadedEvent以及Splash.LoadedEvent的分配上不断获得不正确的参数值。我做错了什么?

Splash s = new Splash();
            DoubleAnimation fade = new DoubleAnimation()
            {
                Duration = new Duration(TimeSpan.FromMilliseconds(3000)),
                From = 1.0,
                To = 0.0,
                RepeatBehavior = new RepeatBehavior(1)
            };

            fade.Completed += new EventHandler(fade_Completed);

            this.popup = new Popup();
            this.popup.Child = s;

            EventTrigger et = new EventTrigger();
            et.RoutedEvent = Popup.LoadedEvent;

            Storyboard sb = new Storyboard();
            sb.Children.Add(fade);

            BeginStoryboard bs = new BeginStoryboard() { Storyboard = sb };

            et.Actions.Add(bs);

            this.popup.Triggers.Add(et);
            this.popup.IsOpen = true;

我似乎也无法弄清楚设置目标属性的位置和方式。

编辑:我能够使用提供的@ Titan2782链接获得答案。我在下面的答案中发布了它。

4 个答案:

答案 0 :(得分:3)

签出http://www.windowsphonegeek.com/articles/wp7-transitions-in-depth--custom-transitions它有一些代码可以使用storyboard并设置目标属性。

答案 1 :(得分:1)

您应该查看Windows Phone工具包中的转换内容:http://blogs.msdn.com/b/wfaught/archive/2010/11/15/transitions.aspx

它只有几行才能获得这些过渡。

您可能在此处遇到一些问题,因为您正在使用弹出窗口,并且可视树中不存在弹出窗口?

答案 2 :(得分:1)

我在vb中有一个按钮的例子,不应该很难翻译成c#:

Dim Fade As New Animation.DoubleAnimation
Fade.From = 0.5
Fade.To = 1
Fade.Duration = TimeSpan.FromSeconds(3)

Animation.Storyboard.SetTarget(Fade, button)
Animation.Storyboard.SetTargetProperty(Fade, New PropertyPath(Button.OpacityProperty))

Dim sb As New Animation.Storyboard
sb.Children.Add(highlight)

sb.Begin()

我认为这也适用于Popup。

答案 3 :(得分:1)

感谢@ Titan2782回答我能够弄清楚

    Splash s = new Splash();
                DoubleAnimation fade = new DoubleAnimation()
                {
                    Duration = new Duration(TimeSpan.FromMilliseconds(4000)),
                    From = 1.0,
                    To = 0.0,
                    RepeatBehavior = new RepeatBehavior(1)

                };

                fade.Completed += new EventHandler(fade_Completed);

                this.popup = new Popup();
                this.popup.Child = s;

                Storyboard.SetTargetProperty(fade, new PropertyPath(UIElement.OpacityProperty));
                sb.Children.Add(fade);
                Storyboard.SetTarget(sb, s);           

                this.popup.IsOpen = true;

                sb.Begin();