在MVVM模式中播放故事板,可以通过以下步骤实现:
以下是一个简单的示例,演示如何在MVVM模式中播放故事板:
public class MainViewModel : INotifyPropertyChanged
{
private bool _isPlaying;
public bool IsPlaying
{
get { return _isPlaying; }
set
{
_isPlaying = value;
OnPropertyChanged("IsPlaying");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<Button Content="Play" IsEnabled="{Binding IsPlaying, Converter={StaticResource BooleanInverter}}"/>
<Button Content="Pause" IsEnabled="{Binding IsPlaying}"/>
</Grid>
</Window>
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:1"/>
</Storyboard>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding PlayCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding IsPlaying}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource PlayPauseStoryboard}"/>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
这个示例中,当用户单击“Play”按钮时,视图模型中的“IsPlaying”属性将更改为True,从而触发故事板中的动画效果。当用户单击“Pause”按钮时,视图模型中的“IsPlaying”属性将更改为False,从而停止动画效果。
领取专属 10元无门槛券
手把手带您无忧上云