MVVM WP7 - 页面导航和过渡动画

时间:2012-10-10 11:46:53

标签: windows-phone-7 mvvm mvvm-light transition

我是WP7中MVVM模型的初学者。我无法实现页面之间的导航以及动画过渡。

我想要实现的目标:

  1. Page1 导航到 Page2 时,将启动过渡动画 animation1 ,但当 Page1 导航至 Page3 将启动不同的过渡动画 animation2

  2. ViewModel可以取消GoBack导航,例如当搜索模式打开时(TextBox可见,并且在用户按下后退键,TextBox将被隐藏,ViewModel将切换到搜索模式,并阻止页面导航回来)

  3. 我创造了这样的东西:

    ExtendedPhoneApplicationPage:PhoneApplicationPage - 特殊控件,继承自PhoneApplicationPage,通过接口INavigation调用对ViewModel的请求

    MainViewModel:INavigation - MainViewModel,它是ViewModel,并实现接口INavigation。

    它如何运作?

    1. 用户点击后退键
    2. ExtendedPhoneApplicationPage询问MainViewModel(或实现INAVigation的不同)现在应该开始什么动画
    3. MainViewModel将正确的动画返回到View
    4. MVVM是否正确?如果没有,怎么能实现这个目标?

      修改

      我上面写的可能不是很好的解决方案,我读到ViewModel应该只与View by Data Binding和Command通信。今天我在想这个,我明白了。

      在XAML中应该是这样的:

      <Navigation>
              <NavigationInTransition>
                  <NavigationInTransition.ForwardTransitions>
                      <ForwardTransition NavigateTo="page2.xaml">
                          <ForwardTransition.Animation>
                              <SlideTransitionAnimation Mode="ForwardIn"/>
                          </ForwardTransition.Animation>
                      </ForwardTransition>
                      <ForwardTransition NavigateTo="page3.xaml">
                          <ForwardTransition.Animation>
                              <TurnstyleTransitionAnimation Mode="ForwardIn"/>
                          </ForwardTransition.Animation>
                      </ForwardTransition>
                  </NavigationInTransition.ForwardTransitions>
              </NavigationInTransition>
              <BackKeyPressed IsPrevent="{Binding SomethingBool}" Command="{Binding BackKeyPressed}"/>
      </Navigation>
      

1 个答案:

答案 0 :(得分:0)

在App.xaml中创建资源

  <Style x:Key="PageStyle" TargetType="phone:PhoneApplicationPage">
  <Setter Property="toolkit:TransitionService.NavigationInTransition">
    <Setter.Value>
      <toolkit:NavigationInTransition>
        <toolkit:NavigationInTransition.Backward>
          <toolkit:TurnstileTransition Mode="BackwardIn" />
        </toolkit:NavigationInTransition.Backward>
        <toolkit:NavigationInTransition.Forward>
          <toolkit:TurnstileTransition Mode="ForwardIn" />
        </toolkit:NavigationInTransition.Forward>
      </toolkit:NavigationInTransition>
    </Setter.Value>
  </Setter>
  <Setter Property="toolkit:TransitionService.NavigationOutTransition">
    <Setter.Value>
      <toolkit:NavigationOutTransition>
        <toolkit:NavigationOutTransition.Backward>
          <toolkit:TurnstileTransition Mode="BackwardOut" />
        </toolkit:NavigationOutTransition.Backward>
        <toolkit:NavigationOutTransition.Forward>
          <toolkit:TurnstileTransition Mode="ForwardOut" />
        </toolkit:NavigationOutTransition.Forward>
      </toolkit:NavigationOutTransition>
    </Setter.Value>
  </Setter>
</Style>

用于转换的页面  Style =“{StaticResource PageStyle}”