Behaviors for WPF in Microsoft Expression Blend 3

来源:互联网 发布:c语言 x 编辑:程序博客网 时间:2024/06/10 15:19

Blend 3 里面有若干个 Behavior,这些 Behavior 可以加入到 UIElement 中:


现在分别看下各个 Behavior, 所有的这些 Behavior classes 的参考可以参考 Microsoft Expression Blend 3 SDK Document. 下面是摘录其中一些内容和自己体验后的一个简短描述。

1. ChangePropertyAction

描述: An action that will change a specified property to a specified value when invoked.

[DefaultTriggerAttribute(typeof(ButtonBase),typeof(EventTrigger), "Click")][DefaultTriggerAttribute(typeof(UIElement), typeof(EventTrigger), "MouseLeftButtonDown")]public class ChangePropertyAction : TargetedTriggerAction<DependencyObject>
所在 namespace: Microsoft.Expression.Interactivity.Core
继承层次:
System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.DependencyObject
       System.Windows.Freezable
         System.Windows.Media.Animation.Animatable
           System.Windows.Interactivity.TriggerAction
             System.Windows.Interactivity.TargetedTriggerAction
               System.Windows.Interactivity.TargetedTriggerAction<(Of <(DependencyObject>)>)
                Microsoft.Expression.Interactivity.Core.ChangePropertyAction
目的是在 某个 Trigger 指定的 Event 触发时在某个 Duration 时间段
将某个 DependencyProperty 的值从 原来 过渡(改变)到 Value 指定的值。
 
体验:更加简化了动画的创建。例如一个 Button, Width=300, 想在 MouseEnter 时 Width 在 3 秒内变为 40,
当 MouseLeave 时 其 Width 变为 300.
<Button Content="Button" Grid.Row="1" Grid.Column="1" Width="300">
<
i:Interaction.Triggers>
   <
i:EventTrigger EventName="MouseEnter">
    <
ic:ChangePropertyAction PropertyName="Width" Value="40" Duration="00:00:03"/>
   </
i:EventTrigger>
   <
i:EventTrigger EventName="MouseLeave">
    <
ic:ChangePropertyAction PropertyName="Width" Value="300" Duration="00:00:03"/>
   </
i:EventTrigger>
</
i:Interaction.Triggers>
</
Button
>
注意的是 Value 的值不要设置为 Nan 或者 Auto, 以前创建动画时不可以,现在也不可以。
可以看到,之前要实现这个类型的 StoryBoard 比这个要麻烦一些。
 
2.ControlStoryboardAction 
描述:An action that will change the state of a targeted storyboard when invoked.
[CLSCompliantAttribute(false)]public class ControlStoryboardAction : StoryboardAction
 
继承的层次:
System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.DependencyObject
       System.Windows.Freezable
         System.Windows.Media.Animation.Animatable
           System.Windows.Interactivity.TriggerAction
             System.Windows.Interactivity.TriggerAction<(Of <(DependencyObject>)>)
               Microsoft.Expression.Interactivity.Media.StoryboardAction
                Microsoft.Expression.Interactivity.Media.ControlStoryboardAction
 
所在的 namespace: Microsoft.Expression.Interactivity.Media
简化了 故事板状态改变 设定时机。
记得之前我在 知道 回答过此类问题:我想在 xxx 事件时才启动/停止 故事板,而不是在 UIElement.Loaded 事件。
Blend 2 创建的故事板默认是当 UIElement.Loaded 事件发生时启动的。有些人不会设置,所以有了上述这种问题。
现在有了这个行为,这种设定变得简单多了。
ControlStoryboardAction 行为在 某个 Trigger 指定的 控制项(由 SourceName 指定)的
某个 Event 发生时改变某个 StoryBoard 的状态(包括 Play, Stop, TogglePlayPause, Pause,
Resume, SkipToFill 六种状态)。这种行为有 IsEnabled 属性,以决定是否启用这种行为。
例如, 当一个 Button 被 Click 后,Key 为 abc 的故事板将处于 Stop 的状态。这里 abc 的 Storyboard 就略去了。
<Button Content="Button" Grid.Row="1" Grid.Column="1" Width="300">
<
i:Interaction.Triggers>
   <
i:EventTrigger EventName="Click">
    <
im:ControlStoryboardAction ControlStoryboardOption="Pause" Storyboard="{StaticResource abc}"/>
   </
i:EventTrigger>
</
i:Interaction.Triggers>
</
Button>
这个设置相当简单,免去了从代码中设定的麻烦,使得 UI Designer 可以完成这种 它和 Coder 以前共同控制的事情。
以前的设置方法:
在 Button 的 Click 事件里:
(....Resources["abc"] as Storyboard).Pause();
 
 
3. 
FluidMoveBehavior

描述: Behavior that watches an element (or a set of elements) for layout changes,
     and moves the element smoothly to the new position when needed.
     This behavior does not animate the size or visibility of an element;
     it only animates the offset of that element within its parent container.
 
public class FluidMoveBehavior : Behavior<FrameworkElement>
 
所在 namespace: Microsoft.Expression.Interactivity.Layout
继承的层次:
System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.DependencyObject
       System.Windows.Freezable
         System.Windows.Media.Animation.Animatable
           System.Windows.Interactivity.Behavior
             System.Windows.Interactivity.Behavior<(Of <(FrameworkElement>)>)
              Microsoft.Expression.Interactivity.Layout.FluidMoveBehavior
 
从描述可知它提供了一种对控制项从某个位置到另一个位置的平滑过渡。(相当于位移的 storyboard).
它有三个 DependencyProperty 可以被设置:
AppliesTo: 被应用的 控制项, 值有 Self(预设) 和 Children, 是 FluidMoveScope 类型值。
Duration: Duration 类型值,指示动画的持续时间。预设为 1 秒 (00:00:01).
IsActive: 是否激活这种行为,True(预设) 或 False.
 
这种简单的动画你不能设置详细的诸如方向(从左到右或从上至下等等)。但也很有趣。
也看个例子, 当某个 控制项出现时,如果 IsActive 不为 False, 动画效果就产生了:
<Grid x:Name="LayoutRoot">
<i:Interaction.Behaviors>
   <il:FluidMoveBehavior AppliesTo="Children"/>
</i:Interaction.Behaviors>
<Grid.RowDefinitions>
   <RowDefinition Height="Auto"/>
   <RowDefinition />
   <RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
   <ColumnDefinition Width="Auto"/>
   <ColumnDefinition />
   <ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Menu>
   <MenuItem Header="File">
    <MenuItem Header="Close"/>
   </MenuItem>
</Menu>
<Button Content="Button" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<StatusBar Grid.Row="2" Grid.ColumnSpan="3">
   <StatusBarItem Content="Ready"/>
</StatusBar>
</Grid>
 
 
4. GoToStateAction
描述: An action that will transition a FrameworkElement to a specified VisualState when invoked.
 
[DefaultTriggerAttribute(typeof(ButtonBase), typeof(EventTrigger), "Click")][DefaultTriggerAttribute(typeof(UIElement), typeof(EventTrigger), "MouseLeftButtonDown")]public class GoToStateAction : TargetedTriggerAction<FrameworkElement>
 
所在 namespace: Microsoft.Expression.Interactivity.Core
继承的层次:
System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.DependencyObject
       System.Windows.Freezable
         System.Windows.Media.Animation.Animatable
           System.Windows.Interactivity.TriggerAction
             System.Windows.Interactivity.TargetedTriggerAction
               System.Windows.Interactivity.TargetedTriggerAction<(Of <(FrameworkElement>)>)
                Microsoft.Expression.Interactivity.Core.GoToStateAction
 
有点无语,VSM 最好不用,所以最好也不用 GoToStateAction, BUG 经常重现. 期待改进的 VSM 再说此 Behavior.
 
 5.LaunchUriOrFileAction
 
专为懒人设计的 Behavior, 设置好 Path 和控制项的事件,将启动 Path 所指的档案,如果 Path 所指定的档案关联到某程式,则启动对应的程式并装载 该档案。
这个行为就类似于 Process.Start, 但是无法设定详细的 StartupInfo.
 
6.MouseDragElementBehavior
这个行为好极了,它允许 添加了此行为的 控制项 Dragable.
之前我们要实现用鼠标拖动一个控制项 的通用类别时,还要考虑是  Margin, Padding, Left,.... 等等一堆烦锁的东西,
而此行为减免了程式设计员的操心. 一个很好的应用就是 可以轻易模拟实现传统的 MDI 窗口.
 
但不足之处是,如果控制项 鼠标按下 时参数 e.Handle 已经为 True, 那么这种控制项将不能使用这个行为(除非使用某些技巧),
例如 Button. 因此如果要实作一个 Dragable 的 Button 还是略显麻烦。