WPF 控件开发之MediaElement

简介:

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,如需转载请自行联系原作者

目录
相关文章
|
1天前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
|
1天前
|
C#
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
68 1
|
9月前
|
C# Windows
WPF技术之RichTextBox控件
WPF RichTextBox是Windows Presentation Foundation (WPF)中提供的一个强大的文本编辑控件,它可以显示富文本格式的文本,支持多种文本处理操作。
360 0
|
1天前
|
前端开发 C# 容器
浅谈WPF之控件拖拽与拖动
使用过office的visio软件画图的小伙伴都知道,画图软件分为两部分,左侧图形库,存放各种图标,右侧是一个画布,将左侧图形库的图标控件拖拽到右侧画布,就会生成一个新的控件,并且可以自由拖动。那如何在WPF程序中,实现类似的功能呢?今天就以一个简单的小例子,简述如何在WPF中实现控件的拖拽和拖动,仅供学习分享使用,如有不足之处,还请指正。
118 2
|
1天前
|
C# 开发者 C++
一套开源、强大且美观的WPF UI控件库
一套开源、强大且美观的WPF UI控件库
147 0
|
6月前
|
算法 C# UED
浅谈WPF之控件模板和数据模板
WPF不仅支持传统的Windows Forms编程的用户界面和用户体验设计,同时还推出了以模板为核心的新一代设计理念。在WPF中,通过引入模板,将数据和算法的“内容”和“形式”进行解耦。模板主要分为两大类:数据模板【Data Template】和控件模板【Control Template】。
105 8
|
6月前
|
程序员 C# 异构计算
一个为程序员定制的、WPF开发的小巧、美观桌面快捷工具
一个为程序员定制的、WPF开发的小巧、美观桌面快捷工具
62 0
|
6月前
|
C# 开发者
一款WPF开发的网易云音乐客户端 - DMSkin-CloudMusic
一款WPF开发的网易云音乐客户端 - DMSkin-CloudMusic
133 36
|
9月前
|
定位技术 C# UED
WPF技术之ScrollViewer控件
WPF ScrollViewer是WPF中常用的一个控件,它提供了滚动视图的功能,可用于显示超出容器可视区域的内容。ScrollViewer通常用于容纳大量内容的控件,以在有限的空间内显示这些内容,并允许用户通过滚动来查看隐藏的部分。
805 0
|
9月前
|
前端开发 C#
WPF技术之ContentControl 控件
ContentControl 是 WPF 中的一个常见控件,用于显示单个内容元素。它可以包含任意类型的内容,包括文本、图像、控件等。
837 0