本文共 3181 字,大约阅读时间需要 10 分钟。
MediaElement的暂停,播放功能。
<UserControl x:Class= "Control_Test.MediaElement" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" > <UserControl.Resources> <ImageBrush x:Key= "StartImgBrush" ImageSource= "Chapter02/player_start.png" /> <ImageBrush x:Key= "PauseImgBrush" ImageSource= "Chapter02/player_pause.png" /> <ImageBrush x:Key= "PlayImgBrush" ImageSource= "Chapter02/player_play.png" /> <ImageBrush x:Key= "StopImgBrush" ImageSource= "Chapter02/player_stop.png" /> <!--Button控件模板--> <ControlTemplate x:Key= "ButtonTemplate" TargetType= "Button" > <Grid> <Rectangle x:Name= "Rect" Fill= "{TemplateBinding Background}" /> </Grid> </ControlTemplate> <ControlTemplate x:Key= "PlayButtonTemplate" TargetType= "ToggleButton" > <Grid> <Rectangle x:Name= "Rect" Fill= "{StaticResource PlayImgBrush}" /> </Grid> <ControlTemplate.Triggers> <Trigger Property= "ToggleButton.IsChecked" Value= "True" > <Setter Property= "Fill" Value= "{StaticResource PauseImgBrush}" TargetName= "Rect" > </Setter> </Trigger> <Trigger Property= "ToggleButton.IsChecked" Value= "False" > <Setter Property= "Fill" Value= "{StaticResource PlayImgBrush}" TargetName= "Rect" > </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </UserControl.Resources> <UserControl.CommandBindings> <CommandBinding Command= "MediaCommands.TogglePlayPause" CanExecute= "CommandBinding_CanExecute" Executed= "PlayPause" /> <CommandBinding Command= "MediaCommands.Stop" CanExecute= "CommandBinding_CanExecute" Executed= "Stop" /> <CommandBinding Command= "MediaCommands.Rewind" CanExecute= "CommandBinding_CanExecute" Executed= "Stop" /> </UserControl.CommandBindings> <DockPanel> <Grid DockPanel.Dock= "Bottom" > <Border Width= "180" Height= "24" Background= "LightGray" BorderBrush= "DarkGray" BorderThickness= "1" CornerRadius= "12" SnapsToDevicePixels= "False" ></Border> <StackPanel Orientation= "Horizontal" HorizontalAlignment= "Center" > <Button Width= "32" Height= "32" Command= "MediaCommands.Rewind" Template= "{StaticResource ButtonTemplate}" Background= "{StaticResource StartImgBrush}" >Rewind</Button> <ToggleButton x:Name= "_playBtn" Width= "48" Height= "48" Margin= "10,0,10,0" Command= "MediaCommands.TogglePlayPause" Template= "{StaticResource PlayButtonTemplate}" /> <Button Width= "32" Height= "32" Command= "MediaCommands.Stop" Template= "{StaticResource ButtonTemplate}" Background= "{StaticResource StopImgBrush}" >Stop</Button> </StackPanel> </Grid> <MediaElement x:Name= "myMedia" LoadedBehavior= "Manual" UnloadedBehavior= "Pause" Source= "E:/Bear.wmv" /> </DockPanel> </UserControl> |
C#Code
private void CommandBinding_CanExecute( object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true ; } private void PlayPause( object sender, ExecutedRoutedEventArgs e) { if (_isPlaying) { myMedia.Pause(); _isPlaying = false ; } else { myMedia.Play(); _isPlaying = true ; } } private void Stop( object sender, ExecutedRoutedEventArgs e) { myMedia.Stop(); _playBtn.IsChecked = false ; _isPlaying = false ; } |
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/09/20/2182334.html,如需转载请自行联系原作者