gpt4 book ai didi

wpf - 使用 WPF MVVM 运行动画

转载 作者:行者123 更新时间:2023-12-04 20:27:19 24 4
gpt4 key购买 nike

使用 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 元素上运行上述动画。该按钮具有命令绑定(bind):

<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 模式,我不想这样做。

最佳答案

如何在按钮单击事件中触发 Storyboard 内的动画:

<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>

关于wpf - 使用 WPF MVVM 运行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18651163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com