3

MVVM が使用されている場合(およびコード ビハインドが回避されている場合)、WPF でアニメーションを実行するオプションは何ですか?

XAML リソースでアニメーションを定義しました。

<Storyboard x:Key="showMe">
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
          <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
     </ObjectAnimationUsingKeyFrames>
     <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0.5" From="0" To="1" />
</Storyboard>

ボタンがクリックされたときに、いくつかの UI 要素で上記のアニメーションを実行したいと考えています。ボタンにはコマンド バインディングがあります。

<Button Name="btnShowImage" Command="{Binding SomeCommand}" />

MVVM:

Public Property SomeCommand As ICommand
    Get
        If _SomeCommand Is Nothing Then _SomeCommand = New RelayCommand(New Action(AddressOf DoSomething))
        Return _SomeCommand 
    End Get
    Set(value As ICommand)
        _SomeCommand  = value
    End Set
End Property
Private _SomeCommand As ICommand

Private Sub DoSomething
     'no access to View from here so how to run the animation?
End Sub

この時点まで、コード ビハインドからアニメーションを実行しました。

Dim stb As Storyboard = TryCast(FindResource("showMe"), Storyboard)
stb.Begin(imgSomeImage)

...しかし、MVVMパターンのためにやりたくないコードビハインドでボタンクリックイベントを処理する必要があります。

4

1 に答える 1

2

ボタンクリックイベント内でストーリーボード内のアニメーションをトリガーするのはどうですか:

<Button>
    OK
    <Button.Triggers>
      <EventTrigger RoutedEvent="Button.Click">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard x:Key="showMe">
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
          <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
     </ObjectAnimationUsingKeyFrames>
     <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0.5" From="0" To="1" />
</Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </Button.Triggers>
  </Button>
于 2013-09-06T07:05:13.647 に答える