我需要更改控件的状态,然后执行一些操作。具体来说,我想在隐藏控件之前运行动画。我想做这样的事情:
VisualStateManager.GoToState(control, "Hidden", true); // wait until the transition animation is finished
ParentControl.Children.Remove(control);
问题是过渡动画是异步运行的,因此在动画启动后控件会立即从视觉树中删除。
那么如何等待动画完成呢?
我需要更改控件的状态,然后执行一些操作。具体来说,我想在隐藏控件之前运行动画。我想做这样的事情:
VisualStateManager.GoToState(control, "Hidden", true); // wait until the transition animation is finished
ParentControl.Children.Remove(control);
问题是过渡动画是异步运行的,因此在动画启动后控件会立即从视觉树中删除。
那么如何等待动画完成呢?
您可以将 Storyboard.Completed 事件处理程序附加到 Storyboard 或将 VisualStateGroup.CurrentStateChanged 事件处理程序附加到 VisualStateGroup:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="SilverlightApplication7.MainPage"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup" >
<VisualState x:Name="Hidden">
<Storyboard Completed="OnHidden">
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="rectangle" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="136" Margin="48,72,0,0" Opacity="0" Stroke="Black" VerticalAlignment="Top" Width="208"/>
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication7
{
public partial class MainPage : UserControl
{
public MainPage()
{
// Required to initialize variables
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this, "Hidden", true);
}
private void OnHidden(object storyboard, EventArgs args)
{
}
}
}
处理此问题的正确方法是监听 VisualStateGroup 上的 CurrentStateChanged 事件,但根据我的经验,它充其量是不可靠的,最坏的情况是损坏。
第二个选项是在 Storyboard 上挂起 Completed 事件,但这个选项有其自身的缺陷。在某些情况下,视觉状态管理器会在内部生成动画,因此您设置的 Completed 事件不会被调用。
实际上可以在代码中附加 Completed 处理程序:
Collection<VisualStateGroup> grps = (Collection<VisualStateGroup>)VisualStateManager.GetVisualStateGroups(this.LayoutRoot);
foreach (VisualStateGroup grp in grps) {
Collection<VisualState> states = (Collection<VisualState>)grp.States;
foreach (VisualState state in states) {
switch (state.Name) {
case "Intro":
state.Storyboard.Completed += new EventHandler(Intro_Completed);break;
}
}
}
此线程的示例:http ://forums.silverlight.net/forums/p/38027/276746.aspx
也使用附加的行为在现场项目中为我工作!尽管我不得不为根 UserControl(在 VisualStateManager.GoToState 中使用)和 LayoutRoot 使用单独的依赖属性来获取实际的 VisualStateGroup 集合,但这有点烦人。