您应该使用我建议的 XAML 创建组本身,然后您必须像这样找到您正在寻找的 VisualStateGroup:
VisualStateGroup visualStateGroupLookingFor = null;
var visualStateGroups = (VisualStateManager.GetVisualStateGroups(LayoutRoot));
foreach (VisualStateGroup state in visualStateGroups) {
if (state.Name == "VisualStateGroupMine") {
visualStateGroupLookingFor = state;
break;
}
}
然后,你必须创建一个新的 VisualState 和 Storyboard 来添加,例如:
var visualState = new VisualState();
var storyBoard = new Storyboard();
现在,创建动画:
var animation = new DoubleAnimation();
animation.To = 10.0;
并设置动画的目标:
//assuming this is instance of class ClassFoo
//and you want to animate it's Width
Storyboard.SetTarget(animation, this);
Storyboard.SetTargetProperty(animation, new PropertyPath(ClassFoo.WidthProperty));
最后将动画添加到情节提要中,为其命名,将其添加到视觉状态组中:
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
就是这样,像往常一样触发它
VisualStateManager.GoToState(this, "CoolNameLikeWidthAnimation", true);