VisualStateManager无法在UserControl上启动动画我正在尝试在WindowsPhone7Silverlight项目中使用VisualStateManager在UserControl上启动动画,但它不起作用。GoToState一直返回false。该代码包含一个VisualState行为,该行为在数据上下文的State属性发生更改时运行GoToState,这在单击UI中的按钮时发生:我做错了什么?XAML:C#:公共类测试:INotifyPropertyChanged{公共事件PropertyChangedEventHandlerPropertyChanged;voidOnPropertyChanged(stringname){if(PropertyChanged!=null)PropertyChanged(this,newPropertyChangedEventArgs(name));}字符串获取_state字符串;公共{返回_state;}设置{_state=值;OnPropertyChanged("状态");}}}publicstaticclassVisualStates{publicstaticreadonlyDependencyPropertyCurrentStateProperty=DependencyProperty.RegisterAttached("CurrentState",typeof(String),typeof(VisualStates),newPropertyMetadata(TransitionToState));publicstaticstringGetCurrentState(DependencyObjectobj){return(string)obj.GetValue(CurrentStateProperty);}publicstaticvoidSetCurrentState(DependencyObjectobj,stringvalue){obj.SetValue(CurrentStateProperty,value);私有静态无效过渡到状态(对象发送者,DependencyPropertyChangedEventArgsargs){Controlc=senderasControl;if(c!=null){boolb=VisualStateManager.GoToState(c,(string)args.NewValue,false);}else{thrownewArgumentException("CurrentState仅支持Control类型");}}publicpartialclassMainPage:PhoneApplicationPage{publicMainPage(){InitializeComponent();}privatevoidPhoneApplicationPage_Loaded(objectsender,RoutedEventArgse){_testSubject.DataContext=newTest();}privatevoidButton_Click(objectsender,RoutedEventArgse){((Test)_testSubject.DataContext).State="State2";只是一个疯狂的猜测,但也许它在错误的线程中执行?您可能希望使用调度程序在正确的(UI)线程上执行GoToState在Button_Click函数中是否起作用?privatevoidButton_Click(objectsender,RoutedEventArgse){boolb=VisualStateManager.GoToState(this,"State2",false);}并在代码执行时调用TransitionToState。这将排除任何其他问题。更新以下对我有用。我在设置背景时遇到了一些问题。首先,这对UserControl没有影响,其次,无法使用Color动画更改背景,这就是我要更改不透明度的原因。MainPage.xaml中的TestControl.xamlTest.cs/TransitionToState方法privatestaticvoidTransitionToState(objectsender,DependencyPropertyChangedEventArgsargs){if(c!=null&&args.NewValue!=null){boolb=VisualStateManager.GoToState(c,(string)args.NewValue,true);变量a=b;}}MainPage.cspublicMainPage(){InitializeComponent();_testSubject.DataContext=new测试();_test2Subject.DataContext=new测试();}privatevoidButton_Click(objectsender,RoutedEventArgse){((Test)_testSubject.DataContext).State="State2";((Test)_test2Subject.DataContext).State="State2";我还建议使用ControlTemplates分配VisualStates而不是直接在控件上定义它们。这会给你更多的灵活性,更好的维护等。希望这有帮助。我遇到了类似的问题,我找到了一个对我有帮助的解决方案,如果用户控件托管在layoutawarepage中,有人可能会发现它很有用,否则你必须遵循(示例可以在layoutawarepage中找到)编辑:抱歉,我认为被误解了这是一个winRT应用程序。以上就是C#学习教程:VisualStateManager无法在UserControl上启动动画分享的全部内容。如果对大家有用,需要了解更多C#学习教程,希望大家多多关注——本文来自网络合集,不代表立场,如涉及侵权,请右击联系管理员删除。如需转载请注明出处:
