silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了silverlight – 根据属性值更改VisualState大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

如何根据WP7上的属性值更改VisualState? 我试图使用MVVM模式,当我的模型加载时,我希望我的视图去特定的VisualState. 在Silverlight中,我们有属性更改的触发器,但在WP7中没有! PS:我不想使用框架,我想了解它是如何在WP7中完成的. 我使用以下附加行为: using System; using System.Windows; using System.Win
如何根据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}">

大佬总结

以上是大佬教程为你收集整理的silverlight – 根据属性值更改VisualState全部内容,希望文章能够帮你解决silverlight – 根据属性值更改VisualState所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。