你應該使用XAML我建議創建集團本身,那麼 你必須找到你正在尋找這樣的VisualStateGroup:
VisualStateGroup visualStateGroupLookingFor = null;
var visualStateGroups = (VisualStateManager.GetVisualStateGroups(LayoutRoot));
foreach (VisualStateGroup state in visualStateGroups) {
if (state.Name == "VisualStateGroupMine") {
visualStateGroupLookingFor = state;
break;
}
}
然後,你必須創建一個新的VisualState和故事板添加,例如:
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));
最後動畫(S)添加到您的故事板,給它一個名稱,將其添加到visualstategroup:
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
完蛋了,觸發它像往常一樣用
VisualStateManager.GoToState(this, "CoolNameLikeWidthAnimation", true);