Skip to content

事件的基本使用方法

  • 事件是一种使对象或者类能够提供通知的成员。

  • 客户端可以通过提供事件处理程序为相应的事件添加可执行代码。

  • 事件是一种特殊的委托。

代码示例:

ConsoleEventArgs.cs

点击查看代码
csharp
using System;

namespace ConsoleEvent
{
    /// <summary>
    /// 自定义事件参数类型
    /// </summary>
    public class ConsoleEventArgs : EventArgs
    {
        // 控制台输出的消息
        private string _message;

        /// <summary>
        /// 构造方法
        /// </summary>
        public ConsoleEventArgs() : base()
        {
            _message = string.Empty;
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="message"></param>
        public ConsoleEventArgs(string message) : base()
        {
            _message = message;
        }

        /// <summary>
        /// 只读属性
        /// </summary>
        public string Message
        {
            get
            {
                return _message;
            }
        }
    }
}

ConsoleMenager.cs

点击查看代码
csharp
using System;

namespace ConsoleEvent
{
    /// <summary>
    /// 管理控制台,在输出前发送输出事件
    /// </summary>
    public class ConsoleMenager
    {
        // 定义控制台事件成员对象
        public event EventHandler<ConsoleEventArgs> ConsoleEvent;

        public void ConsoleOutput(string message)
        {
            // 先发送事件
            ConsoleEventArgs args = new ConsoleEventArgs(message);
            SendConsoleEvent(args);
            // 输出
            Console.WriteLine(message);
        }

        /// <summary>
        /// 负责发送事件
        /// </summary>
        /// <param name="args">事件参数</param>
        protected virtual void SendConsoleEvent(ConsoleEventArgs args)
        {
            // 定义一个临时的引用变量,这样可以确保多线程访问时不会发生问题
            EventHandler<ConsoleEventArgs> temp = ConsoleEvent;
            if (temp != null)
            {
                temp(this, args);
            }
        }
    }
}

Log.cs

点击查看代码
csharp
using System;
using System.IO;

namespace ConsoleEvent
{
    /// <summary>
    /// 日志类型,订阅控制台输出事件
    /// </summary>
    public class Log
    {
        // 日志文件
        private const string LogFile = "D:\\TestLog.txt";
        public Log(ConsoleMenager cm)
        {
            // 订阅控制台输出事件
            cm.ConsoleEvent += WriteLog;
        }

        /// <summary>
        /// 事件处理方法(参数为固定模式)
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="args">事件参数</param>
        private void WriteLog(object sender, ConsoleEventArgs args)
        {
            // 文件不存在的话创建新文件
            if (!File.Exists(LogFile))
            {
                using (FileStream fs = File.Create(LogFile)){ }
            }

            FileInfo info = new FileInfo(LogFile);
            using (StreamWriter sw = info.AppendText())
            {
                sw.WriteLine(DateTime.Now.ToString() + "|" +
                    sender.ToString() + "|" +
                    args.Message);
            }
        }
    }
}

Program.cs

点击查看代码
csharp
using System;

namespace ConsoleEvent
{
    class Program
    {
        static void Main(string args)
        {
            // 测试事件
            ConsoleMenager cm = new ConsoleMenager();
            Log log = new Log(cm);
            cm.ConsoleOutput("测试控制台输出事件");
            cm.ConsoleOutput("测试控制台输出事件");
            cm.ConsoleOutput("测试控制台输出事件");
            Console.Read();
        }
    }
}

文件输出内容

点击查看输出内容
text
2016/1/24 20:59:56|ConsoleEvent.ConsoleMenager|测试控制台输出事件
2016/1/24 20:59:56|ConsoleEvent.ConsoleMenager|测试控制台输出事件
2016/1/24 20:59:56|ConsoleEvent.ConsoleMenager|测试控制台输出事件

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.