1

我制作了一个幻灯片动画,当UserControl 更改C#时触发。Visibility我通过用这样Visibility的新属性替换当前属性来做到这一点。

public new Visibility Visibility 

并且更新基类中的可见性是使用base.Visibility并填充属性中的value字段set,就像这样。

base.Visibility = value;

我还添加了我Storyboard的 in set,并if检查 value 是否为 Visible of Hidden。

当我打开我的 UserControl 时,在播放动画之前会更新可见性,这没有问题。

当我关闭我的 UserControl 时,Visiblity 应该在动画完成后更新,但新Visibility值未在base.Visibility.

我尝试在 Xaml 中使用如本文ObjectAnimationUsingKeyFrames所示设置可见性来执行此操作,但这对于更新我用来打开 UserControl 的按钮图像很慢。

这是我的代码。

public new Visibility Visibility
{
    get { return base.Visibility; }
    set
    {
        double marginNumber = Width * -1;
        Storyboard board = null;

        try
        {
            board = (Storyboard)FindResource("Slide");
            Storyboard.SetTarget(board, this);
        }
        catch (Exception) { }

        if (value == System.Windows.Visibility.Visible)
        {    
            Margin = new Thickness(Margin.Left, Margin.Top, marginNumber, Margin.Bottom);
            base.Visibility = value;
            if (board != null)
            {
                ((ThicknessAnimation)board.Children[0]).To = new Thickness(Margin.Left, Margin.Top, 0, Margin.Bottom);
                ((ThicknessAnimation)board.Children[0]).From = Margin;
                board.Begin();
            }
        }
        else
        {
            if (board != null)
            {
                ((ThicknessAnimation)board.Children[0]).To = new Thickness(Margin.Left, Margin.Top, marginNumber, Margin.Bottom);
                ((ThicknessAnimation)board.Children[0]).From = Margin;
                EventHandler handle = null;
                handle += (s, e) =>
                {
                    board.Stop();
                    board.Completed -= handle;

                    base.Visibility = value; <<-- This one does not update.
                };
                board.Completed += handle;
                board.Begin();
            }
        }
    }
}

为什么动画事件完成后我的可见性没有设置?或者我应该使用我链接的帖子中的方法?

谢谢。

4

0 回答 0