VisualStateManager无法在UserControl上启动动画

我正在尝试使用Windows Phone 7 Silverlight项目中的VisualStateManager在UserControl上启动动画,但它不起作用。 GoToState只是继续返回false。

该代码由VisualState行为组成,当更改datacontext上的State属性时,该行为将运行GoToState,这在单击UI中的按钮时会发生:

我究竟做错了什么?

XAML:

  

C#:

 public class Test : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } string _state; public string State { get { return _state; } set { _state = value; OnPropertyChanged("State"); } } } public static class VisualStates { public static readonly DependencyProperty CurrentStateProperty = DependencyProperty.RegisterAttached("CurrentState", typeof(String), typeof(VisualStates), new PropertyMetadata(TransitionToState)); public static string GetCurrentState(DependencyObject obj) { return (string)obj.GetValue(CurrentStateProperty); } public static void SetCurrentState(DependencyObject obj, string value) { obj.SetValue(CurrentStateProperty, value); } private static void TransitionToState(object sender, DependencyPropertyChangedEventArgs args) { Control c = sender as Control; if (c != null) { bool b = VisualStateManager.GoToState(c, (string)args.NewValue, false); } else { throw new ArgumentException("CurrentState is only supported on the Control type"); } } public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); } private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { _testSubject.DataContext = new Test(); } private void Button_Click(object sender, RoutedEventArgs e) { ((Test)_testSubject.DataContext).State = "State2"; } } 

只是一个疯狂的猜测,但它可能是在错误的线程中执行? 您可能希望使用调度程序在正确的(UI)线程上执行它。

GoToState是否在Button_Click函数中工作?

 private void Button_Click(object sender, RoutedEventArgs e) { bool b = VisualStateManager.GoToState(this, "State2", false); } 

并且在执行代码时调用TransitionToState。

这将排除任何其他问题。

UPDATE

以下适用于我。 我遇到了设置背景的一些问题。 首先,这对UserControl没有任何影响,其次不能使用Color动画更改背景,这就是我改变不透明度的原因。

MainPage.xaml中

                       

TestControl.xaml

              

Test.cs / TransitionToState方法

 private static void TransitionToState(object sender, DependencyPropertyChangedEventArgs args) { UserControl c = sender as UserControl; if (c != null && args.NewValue != null) { bool b = VisualStateManager.GoToState(c, (string)args.NewValue, true); var a = b; } } 

MainPage.cs

 public MainPage() { InitializeComponent(); _testSubject.DataContext = new Test(); _test2Subject.DataContext = new Test(); } private void Button_Click(object sender, RoutedEventArgs e) { ((Test)_testSubject.DataContext).State = "State2"; ((Test)_test2Subject.DataContext).State = "State2"; } 

我还建议使用ControlTemplates分配VisualStates,而不是直接在控件上定义它们。 这将为您提供更大的灵活性,更好的维护等。

希望这可以帮助。

我有一个类似的问题,我发现了一个帮助我的解决方案,如果usercontrol托管在layoutawarepage中,可能会有人发现它很有用

  

否则你将不得不遵循(例子可以在布局识别页面中找到)

  • 在sizechanged上创建事件处理程序

  • 在事件处理程序中使用ApplicationView.Value检查viewstate

  • 使用VisualStateManager.GoToState移动到该状态

编辑:抱歉误解我认为这是winRT App