0

当stackpanel可见性设置为可见状态时,我想要一个调整大小的动画,但不是那样,而是包含stackpanel的边框无休止地闪烁。我不认为我做错了。堆栈面板包含一个 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;
}
4

3 回答 3

0

问题是,当您为边框的高度设置动画时,它将触发 SizeChanged 事件,因此您有一个循环: size changed event>animate height>size changed event> ..
同样,当触发 size changed 事件时,大小更改有已经被放置,所以即使它正在工作,当它返回做动画时,你也会有一点点闪烁。
最后在动画中使用 HEight 会强制进行渲染更新,这样就不会被硬件加速。可能最好的方法是进行平移变换或缩放变换。
例如,您可以在点击事件中直接在 MyPanel 上进行 0 和 1 之间的比例转换。

于 2013-09-15T20:34:07.483 回答
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 大小上为父容器设置动画改变了一些这样的东西

于 2013-09-16T10:14:16.983 回答
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();
    }
}

于 2013-09-16T19:08:20.903 回答