WP7 - 通过代码淡出用户控件,而不是 XAML

发布于 2024-11-08 07:27:11 字数 1024 浏览 0 评论 0原文

我正在尝试弹出一个用户控件,然后在 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 获得答案。我已将其发布在下面的答案中。

I'm trying to popup a user control and then fade it out over 3 seconds. I'm trying to use the following code but I keep getting incorrect parameter value on the assignment of Popup.LoadedEvent as well as Splash.LoadedEvent. What am I doing wrong?

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;

I also cant seem to figure out where/how to set the target property.

Edit: I was able to get the answer using the link @Titan2782 provided. I've posted it in an answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断舍离 2024-11-15 07:27:11

查看 http://www.windowsphonegeek.com/articles/ wp7-transitions-in-deep--custom-transitions 它有一些代码可以与情节提要一起使用并设置目标属性。

check out http://www.windowsphonegeek.com/articles/wp7-transitions-in-depth--custom-transitions it has some code to work with storyboard and set the target properties.

自我难过 2024-11-15 07:27:11

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

只需几行即可完成这些转换。

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

You should look into the transitions stuff in the windows phone toolkit: http://blogs.msdn.com/b/wfaught/archive/2010/11/15/transitions.aspx

its only a couple lines to get those transitions in.

You might have some issues here because you're using a popup, and the popup doesn't exist in the visual tree?

别靠近我心 2024-11-15 07:27:11

我有一个用 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()

我想这也适用于弹出窗口。

I have an example with a button in vb, shouldn't be hard to translate into 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()

I suppose this works also with the Popup.

与他有关 2024-11-15 07:27:11

感谢@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();

Thanks to @Titan2782 answer I was able to figure it out

    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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文