博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF 控件开发之MediaElement
阅读量:5862 次
发布时间:2019-06-19

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

你可能感兴趣的文章
关于烂代码的那些事(上)
查看>>
CTF-i春秋网鼎杯第四场部分writeup
查看>>
[NIOS] 如何Erase EPCS flash內容
查看>>
Oracle EBS-SQL (QA-3):检查已检验未入库.sql
查看>>
设计模式学习,总结于一位博友
查看>>
元类metaclass
查看>>
Android学习笔记-----------布局(四)
查看>>
面向对象第二次博客
查看>>
HDU-1845-Jimmy's Assignment
查看>>
Nexus Maven 私服搭建
查看>>
WIN32界面开发之三:DUI雏形开发(一)
查看>>
牛客练习赛43 B题
查看>>
06 面向对象之:反射, 双下方法
查看>>
转载:如何使用KeyChain保存和获取UDID
查看>>
如果你发现mysql的外键约束不管用了
查看>>
Oracle执行计划与统计信息的一些总结
查看>>
Java中HashMap的实现原理
查看>>
【NOIP2009】靶形数独
查看>>
表单套表单的问题
查看>>
一个小型网游服务器
查看>>