基于 C# 的事件(Event)订阅发布 Demo

基于C#在进行UI交互过程中,常常需要使用事件实现交互操作,如进度条、下拉列表等等

在 C# 中,事件(Event)是一种特殊的委托(Delegate),它提供了一种在应用程序中处理某个操作反馈的方式。事件可用于将对象与处理代码耦合,这使得我们可以在代码中使用面向对象的编程技术。

C# 事件通常由两个部分组成:事件发布者和事件订阅者。事件发布者是发出事件的对象,而事件订阅者则是对事件进行响应的代码块。在事件发布者中,我们定义一个事件,并为该事件声明一个委托类型,最后触发事件并传递相关的参数。当事件被触发时,所有已订阅该事件的方法都会被调用。

笔者以音乐播放器为例,提供了一个Demo:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

public delegate void SongEventHandler(string songName, double timeElapsed);

class MusicPlayer
{
    
    
    public event SongEventHandler SongStarted;
    public event SongEventHandler SongEnded;

    public void PlaySongs(List<Tuple<string, double>> songList)
    {
    
    
        for (int i = 0; i < songList.Count; i++)
        {
    
    
            Tuple<string, double> song = songList[i];
            string songName = song.Item1;
            double duration = song.Item2;

            //事件发布
            if (SongStarted != null)
            {
    
    
                //触发歌曲开始
                SongStarted(songName, duration);
            }

            for (double t = 0; t < duration; t += 1.0)
            {
    
    
                Console.WriteLine("{0} / {1}", t, duration);
                System.Threading.Thread.Sleep(10);//模拟歌曲播放过程
            }
            
            //事件发布
            if (SongEnded != null)
            {
    
    
                //触发歌曲结束
                if (i + 1 == songList.Count)
                {
    
    
                    SongEnded("暂无歌曲", 0.0);
                }
                else
                    SongEnded(songList[i+1].Item1, duration);
            }
        }
    }

}

class UI
{
    
    
    public void UpdateUI(string songName, double timeElapsed)
    {
    
    
        Console.WriteLine("界面更新显示:{0} - {1}  min", songName, timeElapsed);
        //开始操作
    }
}

class Processor
{
    
    
    public void ProcessSong(string songName, double duration)
    {
    
    
        Console.WriteLine("开始处理下一首歌曲:{0}", songName);
        Console.WriteLine();
    }
}

class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        //初始化
        MusicPlayer player = new MusicPlayer();
        UI ui = new UI();
        Processor processor = new Processor();

        //事件订阅:以委托列表的方式载入(+=)事件中,并会在Playsong中触发
        //当前歌曲开始事件
        player.SongStarted += ui.UpdateUI;
        //当前歌曲结束事件
        player.SongEnded += processor.ProcessSong;

        //载入音乐信息<歌名,歌长>
        List<Tuple<string, double>> songList = new List<Tuple<string, double>>{
    
    
            Tuple.Create("Hey Jude", 4.5),
            Tuple.Create("Raindrops Keep Falling On My Head", 3.5),
            Tuple.Create("See you again", 5.0)
        };

        //启动播放器
        player.PlaySongs(songList);

        Console.ReadKey();
    }
}

运行结果

界面更新显示:Hey Jude - 4.5 min
0 / 4.5
1 / 4.5
2 / 4.5
3 / 4.5
4 / 4.5
开始处理下一首歌曲:Raindrops Keep Falling On My Head

界面更新显示:Raindrops Keep Falling On My Head - 3.5 min
0 / 3.5
1 / 3.5
2 / 3.5
3 / 3.5
开始处理下一首歌曲:See you again

界面更新显示:See you again - 5 min
0 / 5
1 / 5
2 / 5
3 / 5
4 / 5
开始处理下一首歌曲:暂无歌曲

猜你喜欢

转载自blog.csdn.net/HeyLoong/article/details/130352860
今日推荐