最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# WPF實(shí)現(xiàn)的語(yǔ)音播放自定義控件

 更新時(shí)間:2021年04月07日 11:50:22   作者:ARM830  
這篇文章主要介紹了C# WPF實(shí)現(xiàn)的語(yǔ)音播放自定義控件,幫助大家更好的理解和學(xué)習(xí)使用c# WPF技術(shù),感興趣的朋友可以了解下

原理很簡(jiǎn)單,利用Path畫(huà)一個(gè)圖,然后用動(dòng)畫(huà)進(jìn)行播放,播放時(shí)間由依賴(lài)屬性輸入賦值與控件內(nèi)部維護(hù)的一個(gè)計(jì)時(shí)器進(jìn)行控制。

控件基本是玩具,無(wú)法作為真實(shí)項(xiàng)目使用。

因?yàn)闆](méi)有設(shè)置播放源,所以編寫(xiě)異步播放源或者實(shí)際播放時(shí)候要將事件引發(fā),是否播放等屬性,事件移到真實(shí)播放事件

非專(zhuān)業(yè)UI,即使知道怎么畫(huà)圖也是畫(huà)的不如意,到底是眼睛會(huì)了,手不行啊。

主界面xaml

<local:VoiceAnimeButton  Height="40" Width="200" IconMargin="5,0,-8,0" HorizontalContentAlignment="Center" CornerRadius="15" VerticalContentAlignment="Center" BorderBrush="Black" IconFill="Black"  BorderThickness="1" Background="Transparent"  VoicePlayTime="0:0:1" >
      <local:VoiceAnimeButton.ContentTemplate>
        <DataTemplate>
          <TextBlock FontSize="10" >
           <Run Text="播放時(shí)間"/>
           <Run Text="{Binding RelativeSource={RelativeSource AncestorLevel=1,AncestorType=local:VoiceAnimeButton,Mode=FindAncestor}, Path=VoicePlayTime}"/>
           <Run Text=" "/>
           <Run Text="狀態(tài): "/>
           <Run Text="{Binding RelativeSource={RelativeSource AncestorLevel=1,AncestorType=local:VoiceAnimeButton,Mode=FindAncestor}, Path=IsVoicePlay}"/>
          </TextBlock>
        </DataTemplate>
      </local:VoiceAnimeButton.ContentTemplate>
    </local:VoiceAnimeButton>

控件設(shè)計(jì)XAML

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:聲音播放動(dòng)畫(huà)">


  <Style TargetType="{x:Type local:VoiceAnimeButton}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:VoiceAnimeButton}">
          <Border Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}" Padding="1">
            <Grid >
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
              </Grid.ColumnDefinitions>
              <Border Margin="{TemplateBinding IconMargin}" >
                <Viewbox>
                  <Path x:Name="VoicePath" Height="{TemplateBinding IconHieght}" Width="{TemplateBinding IconWidth}"  Fill="{TemplateBinding IconFill}" >
                    <Path.Data>
                      <PathGeometry>
                        <PathFigureCollection>
                          M20 20 Q12 45 20 85 l7 -4 Q18 48 27 23 l-7 -3Z
                  M32 29 Q22 45 32 75 l7 -4 Q29 50 38 33 l-6.5 -4
                  M45 35 Q38 48 45 68 l7 -4 Q45 50 52 39 l-7.5 -4
                  M58 41 Q55 49 58 61 l17 -11Z
                        </PathFigureCollection>
                      </PathGeometry>
                    </Path.Data>
                  </Path>
                </Viewbox>
              </Border>
              <ContentPresenter Grid.Column="1"  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Grid>
          </Border>
          <ControlTemplate.Triggers>
            <EventTrigger RoutedEvent="VoicePlayStart">
              <BeginStoryboard x:Name="bs1">
                <Storyboard Storyboard.TargetProperty="Data" Storyboard.TargetName="VoicePath" RepeatBehavior="Forever" Duration="0:0:0.4" BeginTime="0">
                  <ObjectAnimationUsingKeyFrames>
                    <DiscreteObjectKeyFrame KeyTime="0:0:0.1">
                      <DiscreteObjectKeyFrame.Value>
                        <PathGeometry>
                          <PathFigureCollection>
                            M45 35 Q38 48 45 68 l7 -4 Q45 50 52 39 l-7.5 -4
                            M58 41 Q55 49 58 61 l17 -11Z
                          </PathFigureCollection>
                        </PathGeometry>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                    <DiscreteObjectKeyFrame KeyTime="0:0:0.2">
                      <DiscreteObjectKeyFrame.Value>
                        <PathGeometry>
                          <PathFigureCollection>
                            M32 29 Q22 45 32 75 l7 -4 Q29 50 38 33 l-6.5 -4
                            M45 35 Q38 48 45 68 l7 -4 Q45 50 52 39 l-7.5 -4
                            M58 41 Q55 49 58 61 l17 -11Z
                          </PathFigureCollection>
                        </PathGeometry>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                    <DiscreteObjectKeyFrame KeyTime="0:0:0.3">
                      <DiscreteObjectKeyFrame.Value>
                        <PathGeometry>
                          <PathFigureCollection>
                            M20 20 Q12 45 20 85 l7 -4 Q18 48 27 23 l-7 -3Z
                            M32 29 Q22 45 32 75 l7 -4 Q29 50 38 33 l-6.5 -4
                            M45 35 Q38 48 45 68 l7 -4 Q45 50 52 39 l-7.5 -4
                            M58 41 Q55 49 58 61 l17 -11Z
                          </PathFigureCollection>
                        </PathGeometry>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                  </ObjectAnimationUsingKeyFrames>

                </Storyboard>
              </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="VoicePlayEnd">
              <RemoveStoryboard BeginStoryboardName="bs1"/>
            </EventTrigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

控件CS代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace 聲音播放動(dòng)畫(huà)
{
  
  public class VoiceAnimeButton : ContentControl
  {
    static VoiceAnimeButton()
    {
      DefaultStyleKeyProperty.OverrideMetadata(typeof(VoiceAnimeButton), new FrameworkPropertyMetadata(typeof(VoiceAnimeButton)));
    }

    private DispatcherTimer Timer;

    public VoiceAnimeButton()
    {
      Timer = new DispatcherTimer();
      Timer.Tick += Timer_Tick;
      Timer.Interval = TimeSpan.FromSeconds(1);
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
      Timer.Stop();
      IsVoicePlay = false;
      this.RaiseEvent(new RoutedEventArgs(VoicePlayEndEvent, this));

    }

    public static readonly RoutedEvent VoicePlayStartEvent = EventManager.RegisterRoutedEvent("VoicePlayStart", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(VoiceAnimeButton));

    /// <summary>
    /// 聲音播放開(kāi)始事件
    /// </summary>
    public event RoutedEventHandler VoicePlayStart
    {
      add
      {
        this.AddHandler(VoicePlayStartEvent, value);
      }
      remove
      {
        RemoveHandler(VoicePlayStartEvent, value);
      }
    }


    public static readonly RoutedEvent VoicePlayEndEvent= EventManager.RegisterRoutedEvent("VoicePlayEnd", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(VoiceAnimeButton));

    /// <summary>
    /// 聲音播放結(jié)束事件
    /// </summary>
    public event RoutedEventHandler VoicePlayEnd
    {
      add
      {
        AddHandler(VoicePlayEndEvent, value);
      }
      remove
      {
        RemoveHandler(VoicePlayEndEvent, value);
      }
    }

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
      base.OnMouseLeftButtonDown(e);
      IsMouseLeftClick = true;
      Timer.Interval = VoicePlayTime;
      Timer.Start();
      IsVoicePlay = true;
      this.RaiseEvent(new RoutedEventArgs(VoicePlayStartEvent,this));
      
    }
    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
      base.OnMouseLeftButtonUp(e);
      IsMouseLeftClick = false;
    }

    public static readonly DependencyProperty VoicePlayTimeProperty = DependencyProperty.Register("VoicePlayTime", typeof(TimeSpan), typeof(VoiceAnimeButton), new PropertyMetadata(TimeSpan.FromMilliseconds(1000)));
   
    public TimeSpan VoicePlayTime
    {
      get => (TimeSpan)GetValue(VoicePlayTimeProperty);
      set => SetValue(VoicePlayTimeProperty, value);
    }
    public static readonly DependencyProperty IsMouseLeftClickProperty = DependencyProperty.Register("IsMouseLeftClick", typeof(bool), typeof(VoiceAnimeButton),new PropertyMetadata(false));

    public bool IsMouseLeftClick
    {
      get => (bool)GetValue(IsMouseLeftClickProperty);
      set => SetValue(IsMouseLeftClickProperty, value);
    }
    public static readonly DependencyProperty IconWidthProperty = DependencyProperty.Register("IconWidth", typeof(double), typeof(VoiceAnimeButton), new PropertyMetadata(100.0));

    public double IconWidth
    {
      get => Convert.ToDouble(IconWidthProperty);
      set => SetValue(IconWidthProperty, value);
    }
    public static readonly DependencyProperty IconHieghtProperty = DependencyProperty.Register("IconHieght", typeof(double), typeof(VoiceAnimeButton), new PropertyMetadata(100.0));

    public double IconHieght
    {
      get => Convert.ToDouble(IconHieghtProperty);
      set => SetValue(IconHieghtProperty, value);
    }

    public static readonly DependencyProperty IconFillProperty= DependencyProperty.Register("IconFill", typeof(SolidColorBrush), typeof(VoiceAnimeButton), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

    public SolidColorBrush IconFill
    {
      get => GetValue(IconFillProperty) as SolidColorBrush;
      set => SetValue(IconFillProperty, value);
    }

    public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(VoiceAnimeButton), new PropertyMetadata(new CornerRadius(0)));

    public CornerRadius CornerRadius
    {
      get => (CornerRadius)GetValue(CornerRadiusProperty);
      set => SetValue(CornerRadiusProperty, value);
    }
    public static readonly DependencyProperty IconMarginProperty = DependencyProperty.Register("IconMargin", typeof(Thickness), typeof(VoiceAnimeButton), new PropertyMetadata(new Thickness(0.0)));

    public Thickness IconMargin
    {
      get => (Thickness)GetValue(IconMarginProperty);
      set => SetValue(IconMarginProperty, value);
    }

    public static readonly DependencyProperty IsVoicePlayProperty = DependencyProperty.Register("IsVoicePlay", typeof(bool), typeof(VoiceAnimeButton));

    public bool IsVoicePlay
    {
      get => (bool)GetValue(IsVoicePlayProperty);
      set => SetValue(IsVoicePlayProperty, value);
    }
  }
}

以上就是C# WPF實(shí)現(xiàn)的語(yǔ)音播放自定義控件的詳細(xì)內(nèi)容,更多關(guān)于WPF實(shí)現(xiàn)語(yǔ)音播放自定義控件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#中在WebClient中使用post發(fā)送數(shù)據(jù)實(shí)現(xiàn)方法

    C#中在WebClient中使用post發(fā)送數(shù)據(jù)實(shí)現(xiàn)方法

    這篇文章主要介紹了C#中在WebClient中使用post發(fā)送數(shù)據(jù)實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-08-08
  • 詳解C#如何加密解密RAR文件

    詳解C#如何加密解密RAR文件

    這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)加密解密RAR文件的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • c# 將Minio.exe注冊(cè)成windows服務(wù)

    c# 將Minio.exe注冊(cè)成windows服務(wù)

    這篇文章主要介紹了c# 如何將Minio.exe注冊(cè)成windows服務(wù),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • 在C#程序中對(duì)MessageBox進(jìn)行定位的方法

    在C#程序中對(duì)MessageBox進(jìn)行定位的方法

    這篇文章主要介紹了在C#程序中對(duì)MessageBox進(jìn)行定位的方法,針對(duì)圖形化界面進(jìn)行調(diào)試,需要的朋友可以參考下
    2015-07-07
  • C#把文件上傳到服務(wù)器中的指定地址

    C#把文件上傳到服務(wù)器中的指定地址

    這篇文章介紹了C#實(shí)現(xiàn)文件上傳到服務(wù)器指定地址的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#實(shí)現(xiàn)簡(jiǎn)單的聊天窗體

    C#實(shí)現(xiàn)簡(jiǎn)單的聊天窗體

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單的聊天窗體,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 如何利用C#打印九九乘法表

    如何利用C#打印九九乘法表

    這篇文章主要給大家介紹了關(guān)于如何利用C#打印九九乘法表的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • C#Windows窗體設(shè)計(jì)之ContextMenuStrip(鼠標(biāo)右擊菜單)的使用

    C#Windows窗體設(shè)計(jì)之ContextMenuStrip(鼠標(biāo)右擊菜單)的使用

    這篇文章主要介紹了C#Windows窗體設(shè)計(jì)之ContextMenuStrip(鼠標(biāo)右擊菜單)的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • C# for循環(huán)的經(jīng)典案例集錦

    C# for循環(huán)的經(jīng)典案例集錦

    本篇文章主要介紹了關(guān)于for循環(huán)的經(jīng)典案例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-05-05
  • C#微信公眾號(hào)與訂閱號(hào)接口開(kāi)發(fā)示例代碼

    C#微信公眾號(hào)與訂閱號(hào)接口開(kāi)發(fā)示例代碼

    這篇文章主要介紹了C#微信公眾號(hào)與訂閱號(hào)接口開(kāi)發(fā)示例代碼,結(jié)合實(shí)例形式簡(jiǎn)單分析了C#針對(duì)微信接口的調(diào)用與處理技巧,需要的朋友可以參考下
    2016-06-06

最新評(píng)論

淳安县| 中牟县| 滁州市| 西城区| 皋兰县| 锦州市| 东方市| 扎囊县| 旅游| 泰宁县| 错那县| 渝中区| 泗水县| 巴东县| 大埔县| 新乡县| 林州市| 英德市| 钦州市| 武穴市| 扎鲁特旗| 上饶县| 东乡县| 五常市| 五原县| 隆子县| 武陟县| 肇州县| 太保市| 桑植县| 牙克石市| 台中市| 遵义市| 晋江市| 道孚县| 宁海县| 博爱县| 余庆县| 磴口县| 宜城市| 龙陵县|