在的可见性在WPF改变之前做一些事情 [英] Do something before the visibilty is changed in wpf

查看:411
本文介绍了在的可见性在WPF改变之前做一些事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图之前能见度变化倒塌。

做一个动画

我想在/当能见度改变淡出效果来实现淡入淡出。我有淡入没有问题,因为我的动画之前能见度的变化(这是很好)。

下面是我的code现在:

 私人无效LoginOverlay_OnIsVisibleChanged(对象发件人,DependencyPropertyChangedEventArgs E)
{
    如果(this.Visibility == Visibility.Visible)
    {
        //淡入......这是工作
        故事板SB =新的故事板();
        DoubleAnimation是DA =新DoubleAnimation是();
        da.From = 0;
        da.To = 1;
        da.Duration =新的持续时间(TimeSpan.FromSeconds(1));
        sb.Children.Add(DA);
        Storyboard.SetTargetProperty(DA,新的PropertyPath(OpacityProperty));
        Storyboard.SetTarget(DA,这一点);
        sb.Begin();
    }
    其他
    {
        //淡出...不工作
        故事板SB =新的故事板();
        DoubleAnimation是DA =新DoubleAnimation是();
        da.From = 1;
        da.To = 0;
        da.Duration =新的持续时间(TimeSpan.FromSeconds(1));
        sb.Children.Add(DA);
        Storyboard.SetTargetProperty(DA,新的PropertyPath(OpacityProperty));
        Storyboard.SetTarget(DA,这一点);
        sb.Begin();
    }
}


解决方案

在启动控制的知名度已经改为动画折叠或隐藏,这意味着不透明度动画,但你不能看到它发生的控制是看不见的。

你有一个选项是改变控制权交还给可见启动动画前,然后添加一个关键帧动画故事板后1秒(或您的动画渐变的长度)来设置能见度恢复到预期值

i'm trying to do an animation before the visibility changes to collapsed.

I'm trying to accomplish a fade in/fade out effect when the visibility is changed. I have no problem with fade in, because the visibility changes before my animation (which is good).

Here's my code right now:

private void LoginOverlay_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    if (this.Visibility == Visibility.Visible)
    {
        //Fade in ... this is working
        Storyboard sb = new Storyboard();
        DoubleAnimation da = new DoubleAnimation();
        da.From = 0;
        da.To = 1;
        da.Duration = new Duration(TimeSpan.FromSeconds(1));
        sb.Children.Add(da);
        Storyboard.SetTargetProperty(da, new PropertyPath(OpacityProperty));
        Storyboard.SetTarget(da, this);
        sb.Begin();
    }
    else
    {
        //Fade out ... not working
        Storyboard sb = new Storyboard();
        DoubleAnimation da = new DoubleAnimation();
        da.From = 1;
        da.To = 0;
        da.Duration = new Duration(TimeSpan.FromSeconds(1));
        sb.Children.Add(da);
        Storyboard.SetTargetProperty(da, new PropertyPath(OpacityProperty));
        Storyboard.SetTarget(da, this);
        sb.Begin();
    }
}

解决方案

Before you start the Animation the visibility of the control has already changed to Collapsed or Hidden, this means that the opacity will animate but you won't be able to see it happen as the control is invisible.

One option you have is to change the control back to visible before starting the animation, then add a keyframe animation to the storyboard to set the visibility back to its intended value after 1 second (or the length of your fade animation)

这篇关于在的可见性在WPF改变之前做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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