Windows通用應用開發手記-Behavior SDK概述


隨着Visual Studio 2013的發布,New Behavior SDK也一起出現了。和Expression Blend SDK相似,包括各種內置行為(behavior和action),可以用來為你的應用增加交互性,和Blend一起使用時可以無需編寫任何代碼。Behavior SDK使用時唯一要做的就是實現他們的接口。

 

Behavior和Action

 

  • 在應用中,能夠給UI元素添加多個behavior,其中,每個行為(behavior)都有一個方法,當行為被附加到依賴對象時會被調用,從依賴對象移除時也會調用。
  • 而對於action,每個action只有一個確定的方法,且僅當符合特定條件是才會被調用,條件可能是事件(event),也可能是數據狀態,甚至是任何東西。

 

使用時behavior和action會一起使用,behavior指定了action被執行的原因和條件,取代了舊版SDK中的觸發器(Trigger)。

 

命名空間

 

 

SDK內容

 

Behavior SDK內容主要有兩個部分,分別是:

  1. 少量通用的Behavior和Action,可以直接拿來使用,主要在Microsoft.Xaml.Interactions.Core及Microsoft.Xaml.Interactions.Media命名空間下,他們包括:

Actions:

l   CallMethodAction : 當被執行時可以在特殊對象上調用一個方法。

l   ChangePropertyAction :當被執行時可以改變特定屬性的值。

l   GoToStateAction : 當被執行時可以改變特定FrameworkElement的VisualStatus。

l   InvokeCommandAction : 當被執行時可以調用特定的ICommand命令。

l   NavigateToPageAction : 當被執行時可以跳轉到特定的Page頁面。

l   ControlStoryboardAction : 當被執行時可以改變特定故事板的狀態。

l   PlaySoundAction : 當被執行時可以播放特定聲音。

 

Behaviors:

l   DataTriggerBehavior : 當綁定的數據滿足特定條件時,執行相應的Action。

l   EventTriggerBehavior :當偵聽的事件被觸發時,執行相應的Action。

l   IncrementalUpdateBehavior :允許ListView和GridView的內容增量更新的行為,可以在他們的ItemTemplate中使用,以提供平滑的體驗。

 

2.用於實現自定義behavior的接口和工具,主要在Microsoft.Xaml.Interactivity命名空間下,主要使用的接口為IBehavior及IAction。

 

如何使用?

 

最簡單的方法是使用Blend自動生成,當然在xaml中手寫也是完全可以的。

 

 

 

Blend生成的代碼如下,這里我們使用的是CallMethodAction:

 

<Interactivity:Interaction.Behaviors>

                <Core:EventTriggerBehavior EventName="Click">

                    <Core:CallMethodAction

                              TargetObject="{Binding Mode=OneWay}"

                              MethodName="SayHello"/>

                </Core:EventTriggerBehavior>

    </Interactivity:Interaction.Behaviors>

 

其他Action使用示例,示例代碼如下:

  1. NavigateToPageAction
<Interactivity:Interaction.Behaviors>

            <Core:EventTriggerBehavior EventName="Click">

<Core:NavigateToPageAction TargetPage="BehaviorDemo.CallMethodActionDemo"/>

            </Core:EventTriggerBehavior>

</Interactivity:Interaction.Behaviors>

 

2.InvokeCommandAction

 

 <Interactivity:Interaction.Behaviors>

                <Core:DataTriggerBehavior Binding="{Binding InputText}"

                        ComparisonCondition="NotEqual" Value="">

                    <Core:InvokeCommandAction Command="{Binding

                                        ChangeColorCommand, Mode=OneWay}"/>

                </Core:DataTriggerBehavior>

            </Interactivity:Interaction.Behaviors>

 

3.ChangePropertyAction

 

<Interactivity:Interaction.Behaviors>

                   <Core:EventTriggerBehavior EventName="GotFocus">

            <Core:ChangePropertyAction PropertyName="Background"

                                        Value="Purple"/>

        </Core:EventTriggerBehavior>

        <Core:EventTriggerBehavior EventName="LostFocus">

            <Core:ChangePropertyAction PropertyName="Background"

                                        Value="LightGray "/>

              </Core:EventTriggerBehavior>

        </Interactivity:Interaction.Behaviors>

 

4.ControlStoryboardAction

 

<Interactivity:Interaction.Behaviors>

        <Core:EventTriggerBehavior EventName="Click">

            <Media:ControlStoryboardAction

                Storyboard="{StaticResource myStoryboard}"

                ControlStoryboardOption="TogglePlayPause"/>

        </Core:EventTriggerBehavior>

</Interactivity:Interaction.Behaviors>

 

5.GoToStateAction

 

<Interactivity:Interaction.Behaviors>

        <Core:EventTriggerBehavior EventName="PointerPressed">

            <Core:GoToStateAction StateName="Pressed"/>

        </Core:EventTriggerBehavior>

        <Core:EventTriggerBehavior EventName="PointerReleased">

            <Core:GoToStateAction StateName="UnPressed"/>

        </Core:EventTriggerBehavior>

</Interactivity:Interaction.Behaviors>

 

6.PlaySoundAction

 

<Interactivity:Interaction.Behaviors>

        <Core:EventTriggerBehavior>

            <Core:CallMethodAction MethodName="StartTimer"

                TargetObject="{Binding Mode=OneWay}"/>

        </Core:EventTriggerBehavior>

        <Core:DataTriggerBehavior Binding="{Binding CurrentTime.Second}"

                ComparisonCondition="GreaterThanOrEqual" Value="0">

            <Media:PlaySoundAction Source="Assets/beep.wav"/>

        </Core:DataTriggerBehavior>

</Interactivity:Interaction.Behaviors>

 

總結 

 

Behavior SDK的behavior和action組件能夠將特定元素的事件和參數傳遞到相應模型上,這對於使用MVVM框架來說是十分方便的。

更為詳細的內容,可以觀看官方的Behavior SDK文檔,分別如下:

API參考:https://msdn.microsoft.com/zh-cn/library/dn458350.aspx

MSDN博客:http://blogs.msdn.com/b/hanxia/archive/2013/10/17/windows-8-1-store-behavior.aspx

其他資源:http://julmar.com/blog/programming/behaviors-in-windows-8-1-store-apps/

              http://www.timmykokke.com/2013/09/behaviors-sdk/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM