我在C#
制作了一个幻灯片动画,当UserControl的Visibility
发生变化时会触发该动画。我已经通过用这样的新属性替换当前的Visibility
属性来完成此任务。
public new Visibility Visibility
要更新基类中的可见性,请使用base.Visibility
并在属性中填充value
set
字段,如下所示。
base.Visibility = value;
我还在Storyboard
中添加了set
,并if
检查了值是否为隐藏的可见。
当我打开我的UserControl时,可见性会在播放动画之前更新,这没有问题。
当我关闭我的UserControl时,Visiblity会在动画完成后更新,但Visibility
中未设置新的base.Visibility
值。
我已尝试使用ObjectAnimationUsingKeyFrames
在Xaml中执行此操作,如this帖子中所示设置可见性,但这是为了更新我用来打开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();
}
}
}
}
为什么在动画事件完成时未设置我的可见性?或者我应该在我链接的帖子中使用该方法吗?
感谢。