如何根据 WP7 上的属性值更改 VisualState?
我尝试使用 MVVM 模式,当我的模型加载时,我希望我的视图转到特定的 VisualState。
在 Silverlight 中,我们有属性更改的触发器,但在 WP7 中没有!
PS:我不想使用框架我想了解它是如何在 WP7 中完成的。
如何根据 WP7 上的属性值更改 VisualState?
我尝试使用 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}">
最初,DataStateBehavior行为看起来完美匹配,本文甚至专门讨论了将其与 WP7 一起使用。
但是,文章引用的 Codeplex 项目不再具有该行为,并且该行为不在 Expression Blend 中(至少对于 WP7 项目)。
我倾向于在 ViewModel 上公开一个属性,并以编程方式侦听 View 中的更改并相应地更改 Visual State:
在视图的构造函数中:
ViewModelLocator.MainViewModelStatic.PropertyChanged += ViewModelPropertyChanged;
然后创建一个相应地更改状态的事件处理程序:
private void ViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == MainViewModel.SomeProp)
{
// Change the state using the VisualStateManager
}
}
达米安