WPF使用Animation仿WeChat(微信)播放語音消息

来源:https://www.cnblogs.com/yanjinhua/archive/2020/04/07/12655045.html
-Advertisement-
Play Games

效果圖預覽 新建MyCustomControl類。 public class MyCustomControl : Control { private static Storyboard MyStory; private ObjectAnimationUsingKeyFrames MyAnimatio ...


效果圖預覽

 

新建MyCustomControl類。

 public class MyCustomControl : Control
    {
        private static Storyboard MyStory;
        private ObjectAnimationUsingKeyFrames MyAnimation;
        private List<BitmapImage> ImageList;

        private UIElement animation;

        public static readonly DependencyProperty DurationProperty =
             DependencyProperty.Register("Duration", typeof(TimeSpan),
             typeof(MyCustomControl), new PropertyMetadata(null));

        /// <summary>
        /// 動畫時間
        /// </summary>
        public TimeSpan Duration
        {
            get { return (TimeSpan)GetValue(DurationProperty); }
            set { SetValue(DurationProperty, value); }
        }

        public static readonly DependencyProperty IsLitProperty =
             DependencyProperty.Register("IsLit", typeof(bool),
             typeof(MyCustomControl), new PropertyMetadata(false, new PropertyChangedCallback(OnIsLitChanged)));

        /// <summary>
        /// 是否開始播放
        /// </summary>
        public bool IsLit
        {
            get { return (bool)GetValue(IsLitProperty); }
            set { SetValue(IsLitProperty, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            animation = Template.FindName("animation", this) as UIElement;
            if (animation != null && IsLit)
                Animate(animation);

        }

        private static void OnIsLitChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            bool newValue = (bool)e.NewValue;
            if (newValue)
            {
                MyCustomControl c = d as MyCustomControl;
                if (c != null && c.animation != null)
                {
                    c.Animate(c.animation);
                }
            }
            else
            {
                MyStory.Stop();
            }
        }

        private void Animate(UIElement animation)
        {
            int count = 0;//計數
            for (double i = Duration.TotalSeconds; i > 1; i--)
            {
                if (count > 2)
                {
                    count = 0;
                }
                MyAnimation.KeyFrames.Add(
                   new DiscreteObjectKeyFrame()
                   {
                       Value = ImageList[count],
                       KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(Duration.TotalSeconds - i))
                   });
                count++;
            }
            Storyboard.SetTarget(MyAnimation, animation);
            Storyboard.SetTargetProperty(MyAnimation,new PropertyPath(Image.SourceProperty));
            MyStory.Children.Add(MyAnimation);//將動畫添加到動畫板中

            Console.WriteLine($"一共添加:{MyAnimation.KeyFrames.Count} 個 DiscreteObjectKeyFrame。");
            MyStory.Begin();
        }
        public MyCustomControl()
        {
            MyStory = new Storyboard();
            MyAnimation = new ObjectAnimationUsingKeyFrames();
            MyAnimation.FillBehavior = FillBehavior.Stop;

            MyAnimation.Completed += (s, args) => 
            {
                IsLit = false;
            };

            ImageList = new List<BitmapImage>();
            ImageList.Add(new BitmapImage(new Uri("pack://application:,,,/Images/0.png")));
            ImageList.Add(new BitmapImage(new Uri("pack://application:,,,/Images/1.png")));
            ImageList.Add(new BitmapImage(new Uri("pack://application:,,,/Images/2.png")));
            

        }
    }

修改MainWindow.xaml。

<Window x:Class="WpfAnimationWeChat.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfAnimationWeChat"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ControlTemplate x:Key="ct" TargetType="local:MyCustomControl">
            <Image x:Name="animation" Height="20" Width="20" Source="/WpfAnimationWeChat;component/Images/2.png"/>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <Viewbox>
            <Grid Width="1240" Height="768">
                <Grid Height="28" Width="100" MouseLeftButtonDown="Grid_MouseLeftButtonDown">
                    <Rectangle RadiusX="4" RadiusY="4" Fill="#9eea6a" />
                    <StackPanel Orientation="Horizontal" Margin="4,0">
                        <!--可以設置MyCustomControl的Duration 和 IsLit(點擊的時候執行)的{binding}-->
                        <local:MyCustomControl x:Name="AudioPlay" Template="{StaticResource ct}" Duration="0:00:10" IsLit="False"/>
                        <TextBlock Text="10ms”" VerticalAlignment="Center" FontSize="20"/>
                </StackPanel>
            </Grid>
            </Grid>
            
        </Viewbox>
    </Grid>
</Window>

新增資源(3張)。

 

MainWindow.xaml.cs新增Grid_MouseLeftButtonDown。

 

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.AudioPlay.IsLit)
            {
                 this.AudioPlay.IsLit = false;
            }
            else
            {
                this.AudioPlay.IsLit = true;
            }
        }

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 大家可以關註作者的賬號,關註從零開始學Java筆記文集。也可以根據目錄前往作者的博客園博客進行學習。本片文件將基於黑馬程式員就業班視頻進行學習以及資料的分享,並記錄筆記和自己的看法。歡迎大家一起學習和討論。 "【從零開始學Java筆記】目錄" 集合的體繫結構 由於不同的數據結構(數據的組織,存儲方式 ...
  • 大家可以關註作者的賬號,關註從零開始學Java筆記文集。也可以根據目錄前往作者的博客園博客進行學習。本片文件將基於黑馬程式員就業班視頻進行學習以及資料的分享,並記錄筆記和自己的看法。歡迎大家一起學習和討論。 "【從零開始學Java筆記】目錄" Set類 Set集合的特點: 無序(存儲和讀取的順序有可 ...
  • 大家可以關註作者的賬號,關註從零開始學Java筆記文集。也可以根據目錄前往作者的博客園博客進行學習。本片文件將基於黑馬程式員就業班視頻進行學習以及資料的分享,並記錄筆記和自己的看法。歡迎大家一起學習和討論。 "【從零開始學Java筆記】目錄" 學生管理系統一直是大學學習過程中無法跳過的環節,因為這個 ...
  • 大家一直都在談論微服務架構,園子裡面也有很多關於微服務的文章,前幾天也有一些園子的朋友問我微服務架構的一些技術,我這裡就整理了微服務架構的技術棧路線圖,這裡就分享出來和大家一起探討學習,同時讓新手對微服務相關技術有一個更深入的瞭解。 ...
  • 1、前言 微服務架構已成為目前互聯網架構的趨勢,關於微服務的討論,幾乎是各大技術論壇、技術大會的熱門話題。而Surging是高性能的模塊化微服務引擎,是大家首選微服務引擎架構之一,而針對於框架有個突出的缺點就是只能支持基於.NET CORE開發,而現如今各大公司開發語言是多樣的,每個業務線有各自開發 ...
  • 項目介紹 Blazor 是一個使用 .NET 生成互動式客戶端 Web UI 的框架: 使用 C# 代替 JavaScript 來創建豐富的互動式 UI。 共用使用 .NET 編寫的伺服器端和客戶端應用邏輯。 將 UI 呈現為 HTML 和 CSS,以支持眾多瀏覽器,其中包括移動瀏覽器。 使用 .N ...
  • 儘管可在任意WPF項目中編寫自定義元素,但通常希望在專門的類庫程式集(DLL)中放置自定義元素。這樣,可在多個WPF應用程式之間共用自定義元素。 為確保具有正確的程式集引用和名稱空間導入,當在Visual Studio中創建應用程式時,應當選擇Custom Control Library(WPF)項 ...
  • 在控制項模板和為其提供支持的代碼之間又一個隱含約定。如果使用自定義控制項模板替代控制項的標準模板,就需要確保新模板能夠滿足控制項的實現代碼的所有需要。 在簡單控制項中,這個過程比較容易,因為對模板幾乎沒有(或完全沒有)什麼真正的需求。對於複雜控制項,問題就顯得有些微妙了,因為控制項的外觀和實現不可能完全相互獨立的 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...