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パターンのためにやりたくないコードビハインドでボタンクリックイベントを処理する必要があります。