zoukankan      html  css  js  c++  java
  • 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;
    
            }
    

      

     

      

  • 相关阅读:
    Perl-统计某电路面积、功耗占比(NVDIA2019笔试)
    Tensorflow 之 loss
    Verilog-同步FIFO
    Verilog-case、casez和casex的区别
    modelsim使用命令
    进制转换工具
    串口写入和读取数据
    串口发送数据——字符串发送与十六进制发送的区别
    字符转换为十六进制 字符串转化为字符数组
    VS2008编了个MFC对话框,编译链接都没有问题,但是运行出来的对话框完全不能点击
  • 原文地址:https://www.cnblogs.com/linlf03/p/2182334.html
Copyright © 2011-2022 走看看