silverlight - 根据属性值更改 VisualState

标签 silverlight windows-phone-7 mvvm visualstatemanager

如何根据 WP7 上的属性值更改 VisualState?

我尝试使用 MVVM 模式,当我的模型加载时,我希望我的 View 转到特定的 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 ) );
        }
    }
}

在您的 View 模型中,公开当前视觉状态的属性,然后在要为您处理视觉状态的视觉元素上使用以下内容绑定(bind)视觉状态,例如
<phone:PhoneApplicationPage ...
    xmlns:common="clr-namespace:PixelLab.Common;assembly=PixelLab.Common"
    common:VisualStates.CurrentState="{Binding CurrentState}">

关于silverlight - 根据属性值更改 VisualState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5008625/

相关文章:

c# - 如何为 PathGeometry 设置动画以使其缓慢显示?

silverlight - sllauncher.exe 命令行选项是否有任何文档?

c# - 在客户端运行时更新 Silverlight 应用程序

c# - 指南针 UI 和指针

javascript - 剑道网格层次结构数据不显示

c# - Silverlight 5 和 dll

wcf - 如何使用azure和wp7将图片上传到blob

c# - 是否可以在不使用任何实际电子邮件帐户的情况下以编程方式发送电子邮件

C# RaisePropertyChanging?

javascript - 绑定(bind)不适用于 KnockoutJS 中 JSON 加载的嵌套模板