在SizeChanged事件上调整动画大小不起作用

时间:2013-09-15 19:15:31

标签: c# silverlight windows-phone-7

当stackpanel可见性设置为可见状态时,我想要一个调整大小的动画,但不是那样,我得到包含stackpanel的边界的无休止闪烁。我不认为我做错了。 Stack面板包含一个TextBlock实例。

private void MyBorder_SizeChanged_1(object sender, SizeChangedEventArgs e)
{
    if (!first)
    {
        DoubleAnimation anim = new DoubleAnimation();
        anim.From = e.PreviousSize.Height;
        anim.To = e.NewSize.Height;
        anim.Duration = new Duration(TimeSpan.FromSeconds(1));
        Storyboard.SetTarget(anim, MyBorder);
        Storyboard.SetTargetProperty(anim, new PropertyPath(Border.HeightProperty));
        Storyboard st = new Storyboard();
        st.Children.Add(anim);
        st.Begin();
    }
    first = false;
}

private void MyBorder_Tap_1(object sender, GestureEventArgs e)
{
    if (MyPanel.Visibility == Visibility.Collapsed)
        MyPanel.Visibility = Visibility.Visible;
    else
        MyPanel.Visibility = Visibility.Collapsed;
}

3 个答案:

答案 0 :(得分:0)

问题在于,当您设置边框高度的动画时,它将触发SizeChanged事件,因此您有一个循环:尺寸更改事件>动画高度>尺寸更改事件> .. 此外,当触发大小更改的事件时,已经进行了大小更改,因此即使它正常工作,当它返回执行动画时,您将获得一点点轻弹。
最后在动画中使用HEight强制渲染更新,这样就不会加速硬件。 可能最好的做法是翻译变换或缩放变换 例如,您可以在点击事件中直接在MyPanel上进行0到1之间的比例变换。

答案 1 :(得分:0)

这可能对你有帮助。这是一项工作

private void MyBorder_SizeChanged_1(object sender, SizeChangedEventArgs e)
    {
        if (!first)
        {
            DoubleAnimation anim = new DoubleAnimation();
            anim.From = e.PreviousSize.Height;
            anim.To = e.NewSize.Height;
            anim.Duration = new Duration(TimeSpan.FromSeconds(1));
            Storyboard.SetTarget(anim, MyBorder);
            Storyboard.SetTargetProperty(anim, new PropertyPath(Border.HeightProperty));
            Storyboard st = new Storyboard();
            st.Children.Add(anim);
            st.Completed += st_Completed;
            MyBorder.SizeChanged -= MyBorder_SizeChanged_1;
            st.Begin();

        }
        first = false;
    }

    void st_Completed(object sender, EventArgs e)
    {
        MyBorder.SizeChanged += MyBorder_SizeChanged_1;
    }

    private void MyBorder_Tap_1(object sender, GestureEventArgs e)
    {
        if (MyPanel.Visibility == Visibility.Collapsed)
            MyPanel.Visibility = Visibility.Visible;
        else
            MyPanel.Visibility = Visibility.Collapsed;


    }

参考即使在故事板中调整边框大小的事实,最好删除大小更改的事件。尝试在chld大小上设置父容器的动画,改变了一些事情

答案 2 :(得分:0)

我已经解决了这个问题。傻我的,我认为StackPanel的Measure方法是私有的,并没有费心去确定它,这里是用于在Click上扩展StackPanel的解决方案代码。

private void MyBorder_Tap_1(object sender, GestureEventArgs e)
{
    if (MyPanel.Visibility == Visibility.Collapsed)
    {
        MyPanel.Visibility = Visibility.Visible;
        MyPanel.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
        DoubleAnimation anim = new DoubleAnimation();
        anim.From = MyBorder.ActualHeight;
        anim.To = MyBorder.ActualHeight + MyPanel.DesiredSize.Height;
        anim.Duration = new Duration(TimeSpan.FromSeconds(0.25));
        Storyboard.SetTarget(anim, MyBorder);
        Storyboard.SetTargetProperty(anim, new PropertyPath(Border.HeightProperty));
        Storyboard st = new Storyboard();
        st.Children.Add(anim);
        st.Begin();
    }
    else
    {
        MyPanel.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
        DoubleAnimation anim = new DoubleAnimation();
        anim.From = MyBorder.ActualHeight;
        anim.To = MyBorder.ActualHeight - MyPanel.DesiredSize.Height;
        anim.Duration = new Duration(TimeSpan.FromSeconds(0.25));
        Storyboard.SetTarget(anim, MyBorder);
        Storyboard.SetTargetProperty(anim, new PropertyPath(Border.HeightProperty));
        Storyboard st = new Storyboard();
        st.Children.Add(anim);
        st.Completed += (a,b) => { MyPanel.Visibility = Visibility.Collapsed; };
        st.Begin();
    }
}

private void MyBorder_Tap_1(object sender, GestureEventArgs e) { if (MyPanel.Visibility == Visibility.Collapsed) { MyPanel.Visibility = Visibility.Visible; MyPanel.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); DoubleAnimation anim = new DoubleAnimation(); anim.From = MyBorder.ActualHeight; anim.To = MyBorder.ActualHeight + MyPanel.DesiredSize.Height; anim.Duration = new Duration(TimeSpan.FromSeconds(0.25)); Storyboard.SetTarget(anim, MyBorder); Storyboard.SetTargetProperty(anim, new PropertyPath(Border.HeightProperty)); Storyboard st = new Storyboard(); st.Children.Add(anim); st.Begin(); } else { MyPanel.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); DoubleAnimation anim = new DoubleAnimation(); anim.From = MyBorder.ActualHeight; anim.To = MyBorder.ActualHeight - MyPanel.DesiredSize.Height; anim.Duration = new Duration(TimeSpan.FromSeconds(0.25)); Storyboard.SetTarget(anim, MyBorder); Storyboard.SetTargetProperty(anim, new PropertyPath(Border.HeightProperty)); Storyboard st = new Storyboard(); st.Children.Add(anim); st.Completed += (a,b) => { MyPanel.Visibility = Visibility.Collapsed; }; st.Begin(); } }