我试图使用MVVM模式,当我的模型加载时,我希望我的视图去特定的VisualState.
在Silverlight中,我们有属性更改的触发器,但在WP7中没有!
PS:我不想使用框架,我想了解它是如何在WP7中完成的.
解决方法 我使用以下附加行为:using System;using System.windows;using System.windows.Controls;namespace PixelLab.WP7.Common.Behaviors{ /// /// ProvIDes an attached behavior for binding visual states. /// public static class VisualStates { /// /// IDentifIEs the CurrentState attached property. /// public static Readonly DependencyProperty CurrentStateProperty = DependencyProperty .Registerattached( "CurrentState",typeof(string),typeof(VisualStates),new PropertyMetadata(TransitionToState)); /// /// Gets the current visual state of the specifIEd object. This is an attached property. /// /// The source object. /// The current visual state of the specifIEd object. public static string GetCurrentState(DependencyObject obj) { return (string)obj.GetValue(CurrentStateProperty); } /// /// Sets the current visual state of the specifIEd object. This is an attached property. /// /// The target object. /// The new visual state. public static voID SetCurrentState(DependencyObject obj,string value) { obj.SetValue(CurrentStateProperty,value); } static voID startOnGuiThread( Action act ) { var disp = Deployment.Current.dispatcher; if( disp.CheckAccess() ) act(); else disp.BeginInvoke( act ); } private static voID TransitionToState( object sender,DependencyPropertyChangedEventArgs args ) { FrameworkElement elt = sender as FrameworkElement; if( null == elt ) throw new ArgumentException( "CurrentState is only supported on the FrameworkElement" ); string newState = args.NewValue.ToString(); startOnGuiThread( () => Extendedvisualstatemanager.GotoElementState( elt,newState,true ) ); } }}
在视图模型中,公开当前可视状态的属性,然后在要处理可视状态的可视元素上使用以下内容绑定可视状态,例如
<phone:PhoneApplicationPage ... xmlns:common="clr-namespace:PixelLab.Common;assembly=PixelLab.Common" common:VisualStates.CurrentState="{Binding CurrentState}">总结
以上是内存溢出为你收集整理的silverlight – 根据属性值更改VisualState全部内容,希望文章能够帮你解决silverlight – 根据属性值更改VisualState所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)