zoukankan      html  css  js  c++  java
  • 事件通知实现界面间的数据交互

    不同界面间的数据交互,常用到的方式是委托。对于简单的业务逻辑,有的可以直接弹窗,在弹窗关闭之后更新处理。但当数据涉及到的界面是多个的时候,可能就会不好处理了。特别是当开发不断深入,直接更新就会显得越来越无力。那有没有一种方式可以处理呢?我们可以通过事件注册与通知的方式来处理。为了方便实现,我们设计了一个简单的接口,代码如下

     /// <summary>
        /// 事件通知
        /// </summary>
        public interface IEventNotify
        {
            #region 属性
            /// <summary>
            /// 行为列表,保存注册的action
            /// </summary>
            List<Action<object>> NotifyActionList { get; set; }
            #endregion
    
            /// <summary>
            ///注册通知
            /// </summary>
            /// <param name="action"></param>
            void RegisterNotify(Action<object> action);
    
            /// <summary>
            /// 取消通知
            /// </summary>
            /// <param name="action"></param>
            void UnregisterNotify(Action<object> action);
    
            /// <summary>
            /// 通知
            /// </summary>
            void Notify(object obj);
        }
    有了这个接口之后,需要实现的地方,只要实现该接口即可,示例代码如下

     public class MyData : IEventNotify
        {
            public List<Action<object>> NotifyActionList { get; set; }
    
            public void Notify(object obj)
            {
                if (ListHelper.IsEmpty(NotifyActionList))//ListHelper代码在后面
                {
                    return;
                }
    
                foreach (var action in NotifyActionList)
                {
                    action?.Invoke(obj);
                }
            }
    
            public void RegisterNotify(Action<object> action)
            {
                if (action == null)
                {
                    return;
                }
                if (ListHelper.IsEmpty(NotifyActionList))
                {
                    NotifyActionList = new List<Action<object>>();
                }
                NotifyActionList.Add(action);
            }
    
            public void UnregisterNotify(Action<object> action)
            {
                if (action == null || ListHelper.IsEmpty(NotifyActionList))
                {
                    return;
                }
                NotifyActionList.Remove(action);
            }
        }
    如果有大量的数据需要这样交互,那么可以把MyData当成基类,其他类直接继承即可。但是有时,因为A类已经继承自B类了,不可能再继承自MyData类,只能是去实现IEventNotify接口,这时RegisterNotify、UnregisterNotify、Notify等内部的代码需要重新实现一次,那有什么方式可以简化呢?

    有的!

    我们可以把RegisterNotify、UnregisterNotify、Notify的内部逻辑代码,作为IEventNotify的扩展方法,这样其他实现IEventNotify接口的地方,代码就可以简化了,代码如下

      public static class EventNotifyExtension
        {
            #region RegisterNotifyDefault
            /// <summary>
            /// 注册通知
            /// </summary>
            /// <param name="action"></param>
            public static void RegisterNotifyDefault(this IEventNotify eventNotifyInstance, Action<object> action)
            {
                if (action == null)
                {
                    return;
                }
                if (ListHelper.IsEmpty(eventNotifyInstance.NotifyActionList))
                {
                    eventNotifyInstance.NotifyActionList = new List<Action<object>>();
                }
                eventNotifyInstance.NotifyActionList.Add(action);
            }
            #endregion
    
            #region UnregisterNotifyDefault
            /// <summary>
            /// 注销通知
            /// </summary>
            /// <param name="action"></param>
            public static void UnregisterNotifyDefault(this IEventNotify eventNotifyInstance, Action<object> action)
            {
                if (action == null || ListHelper.IsEmpty(eventNotifyInstance.NotifyActionList))
                {
                    return;
                }
                eventNotifyInstance.NotifyActionList.Remove(action);
            }
            #endregion
    
            #region NotifyDefault
            /// <summary>
            /// 通知
            /// </summary>
            public static void NotifyDefault(this IEventNotify eventNotifyInstance, object obj)
            {
                if (ListHelper.IsEmpty(eventNotifyInstance.NotifyActionList))
                {
                    return;
                }
    
                foreach (var action in eventNotifyInstance.NotifyActionList)
                {
                    action?.Invoke(obj);
                }
            }
            #endregion
        }
    有了这些扩展方法后,MyData的类的代码就可以简化了,新的实现代码如下

      public class MyData : IEventNotify
        {
            public List<Action<object>> NotifyActionList { get; set; }
    
            public void Notify(object obj)
            {
                this.NotifyDefault(obj);
            }
    
            public void RegisterNotify(Action<object> action)
            {
                this.RegisterNotifyDefault(action);
            }
    
            public void UnregisterNotify(Action<object> action)
            {
                this.UnregisterNotifyDefault(action);
            }
        }
    当然,如果事件注册需要有其他的逻辑,那么就不用扩展方法,而自己去实现相关的逻辑。

    ListHelper代码

     /// <summary>
        /// list帮助类
        /// </summary>
        public static class ListHelper
        {
            #region IsEmpty Function
            /// <summary>
            /// List是否为空,即为null或者Count为零
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="list"></param>
            /// <returns></returns>
            public static bool IsEmpty<T>(IList<T> list)
            {
                if (list == null || list.Count <= 0)
                {
                    return true;
                }
                return false;
            }
            #endregion
    }
    示例效果图如下

    示例的相关代码

      public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MyData myData = new MyData();
                myData.RegisterNotify(UpdateData);
                Form2 form = new Form2(myData);
                form.ShowDialog();
            }
    
            private void UpdateData(object obj)
            {
                textBox1.Text = $"{obj}";
            }
        }

     public partial class Form2 : Form
        {
            #region 内部变量
            private MyData _myData = null;
            #endregion
    
            public Form2(MyData myData)
            {
                InitializeComponent();
                _myData = myData;
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                _myData.Notify(textBox1.Text);
            }
        }
    源码下载http://download.csdn.net/detail/xxdddail/9850746
    转载请注明出处。






  • 相关阅读:
    mysql中的几种join 及 full join问题
    MySQL基础练习题
    SQL之IFNULL()
    SQL之查找表中字段的值相同的记录
    Mysql之将一张表内容导入另一张表中
    selenium无界面操作浏览器与Chrome Options的启动项设置
    SQL UNION 和 UNION ALL 操作符
    Python断言方法assert
    Python标准库--contextlib模块
    Python标准库--itertools模块
  • 原文地址:https://www.cnblogs.com/sparkleDai/p/7604888.html
Copyright © 2011-2022 走看看