//MVVM要事件绑定,记得项目引用类库“Sysrem.Windows.interactivity”,然后XAML引用 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<MediaElement Canvas.Left="0" Canvas.Top="10" Name="MediaElement" Width="300" Height="350" ClipToBounds="True" Source="{Binding GifUrl}" > <i:Interaction.Triggers> <i:EventTrigger EventName="MediaEnded"> <i:InvokeCommandAction Command="{Binding MediaEndedCommand}" CommandParameter="{Binding ElementName=MediaElement}"/> </i:EventTrigger> </i:Interaction.Triggers> </MediaElement>
public ICommand MediaEndedCommand { get { return new DelegateCommand<object>((sender) => { MediaElement media = (MediaElement)sender; media.LoadedBehavior = MediaState.Manual; media.Position = TimeSpan.FromMilliseconds(1); media.Play(); }); } }