使用winforms实现观察者模式

时间:2010-10-29 08:00:10

标签: c# winforms design-patterns

我有一个对象集合,我的许多表单(使用WeifenLuo.WinFormsUI.Docking)需要与之交互。

即。如果集合在一个表单中添加(或删除),则其他表单通过刷新其视图进行响应。

显然,观察者模式在这里是一个很好的候选人。但是,我在尝试在我的程序中实现这个问题时遇到了问题。

首先,最好为我的集合创建一个观察者类,如下所示:

public class DataCollectionObserver : Form
{
    internal static void DataCollectionRegister(DataCollection dataCollection)
    {
        dataCollection.ImageAdded += new EventHandler(dataAdded);
        dataCollection.ImageRemoved += new EventHandler(dataRemoved);
        dataCollection.ImageIndexChanged += new EventHandler(dataIndexChanged);
        dataCollection.ImageListCleared += new EventHandler(dataListCleared);
    }

    internal static void DataCollectionUnRegister(DataCollection dataCollection)
    {
        dataCollection.ImageAdded -= new EventHandler(dataAdded);
        dataCollection.ImageRemoved -= new EventHandler(dataRemoved);
        dataCollection.ImageIndexChanged -= new EventHandler(dataIndexChanged);
        dataCollection.ImageListCleared -= new EventHandler(dataListCleared);
    }

    internal static void dataAdded(object sender, EventArgs e) {}
    internal static void dataRemoved(object sender, EventArgs e) {}
    internal static void dataIndexChanged(object sender, EventArgs e) {}
    internal static void dataListCleared(object sender, EventArgs e) {}
}

然后覆盖子类表单中的基本事件处理程序?

但是,我不能这样做并使用WeifenLuo.WinFormsUI.Docking库......

好吧,我可以让DataCollectionObserver从WeifenLuo.WinFormsUI.Docking继承DockContent,但这会产生一种情况,我需要有两个DataCollectionObserver类 - 一个继承Form,另一个继承DockContent: - [ 或者,我可以使DataCollectionObserver成为一个接口,但仍然留下了重复的代码......

那么,有没有人在这里提出建议?我是否遗漏了一些显而易见的事情,或者为了简单起见,这是否必须重复执行代码?


修改://

我在表单中收到通知时遇到问题。事实上,整个事情现在正在发挥作用。我问的原因是因为整个事情“闻起来”是由于我在这四种不同形式的代码块复制和粘贴,我订阅了集合事件并取消订阅Form.Closing()。

我想要做的是在一个地方实现我复制并粘贴到这四个表单的行为,并让应该收到集合更改通知的表单根据需要实现该行为。

希望让事情更清楚?

FWIW,这是我的收藏类:

using System;
using System.Collections;
using System.Reflection;

namespace MyNameSpace.Collections
{
    /// <summary>
    /// Generic Collection of Objects with Events
    /// </summary>
    public class CollectionWithEvents<T> : CollectionBase
    {
        public bool SuppressEventNotification
        {
            get;
            set;
        }

        public CollectionWithEvents()
        {
            SuppressEventNotification = false;
        }

        #region Events
        /// <summary>
        /// Raises before an item is added to the list.
        /// </summary>
        public event EventHandler<ItemEventArgs<T>> BeforeItemAdded;

        /// <summary>
        /// Raises when an item is added to the list.
        /// </summary>
        public event EventHandler<ItemEventArgs<T>> ItemAdded;

        /// <summary>
        /// Raises before a collection of items is added to the list.
        /// </summary>
        public event EventHandler<ItemsEventArgs<T>> BeforeItemsAdded;

        /// <summary>
        /// Raises when a collection of items is added to the list.
        /// </summary>
        public event EventHandler<ItemsEventArgs<T>> ItemsAdded;

        /// <summary>
        /// Raises before an item is changed in the list.
        /// </summary>
        public event EventHandler<ItemEventArgs<T>> BeforeItemChanged;

        /// <summary>
        /// Raises when an item is changed in the list.
        /// </summary>
        public event EventHandler<ItemEventArgs<T>> ItemChanged;

        /// <summary>
        /// Raises before an item is removed from the list.
        /// </summary>
        public event EventHandler<ItemEventArgs<T>> BeforeItemRemoved;

        /// <summary>
        /// Raises when an item is removed from the list.
        /// </summary>
        public event EventHandler<ItemEventArgs<T>> ItemRemoved;

        /// <summary>
        /// Raises when the items are cleared from the list.
        /// </summary>
        public event EventHandler<EventArgs> ItemsCleared;
        #endregion

        public T this[int index]
        {
            get { return (T)this.List[index]; }
            set
            {
                if (!SuppressEventNotification)
                {
                    OnBeforeItemChanged(this, new ItemEventArgs<T>(value));
                }

                this.List[index] = value;

                if (!SuppressEventNotification)
                {
                    OnItemChanged(this, new ItemEventArgs<T>(value));
                }
            }
        }

        public int Add(T item)
        {
            if (!SuppressEventNotification)
            {
                OnBeforeItemAdded(this, new ItemEventArgs<T>(item));
            }

            int retValue = this.List.Add(item);

            if (!SuppressEventNotification)
            {
                OnItemAdded(this, new ItemEventArgs<T>(item));
            }

            return retValue;
        }

        public void AddRange(Collection<T> collection)
        {
            T[] tmp = new T[collection.Count];

            collection.CopyTo(tmp, 0);

            AddRange(tmp);
        }

        public void AddRange(T[] collection)
        {
            if (!SuppressEventNotification)
            {
                OnBeforeItemsAdded(this, new ItemsEventArgs<T>(collection));
            }

            this.AddRange(collection);

            if (!SuppressEventNotification)
            {
                OnItemsAdded(this, new ItemsEventArgs<T>(collection));
            }
        }

        public bool Contains(T item)
        {
            return this.List.Contains(item);
        }

        public void CopyTo(Array array, int index)
        {
            this.List.CopyTo(array, index);
        }

        public int IndexOf(T item)
        {
            return this.List.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            this.List.Insert(index, item);
        }

        public void Remove(T item)
        {
            if (!SuppressEventNotification)
            {
                OnBeforeItemRemoved(this, new ItemEventArgs<T>(item));
            }

            T tmp = (T)item;

            this.List.Remove(item);

            if (!SuppressEventNotification)
            {
                OnItemRemoved(this, new ItemEventArgs<T>(tmp));
            }

            tmp = default(T);
        }

        public void Sort(string Property, Common.SortOrder Order)
        {
            Common.GenericComparer genericComparer = new Common.GenericComparer(Property, Order);
            this.InnerList.Sort(genericComparer);
        }

        #region Event Methods
        /// <summary>
        /// Raised before an Item is added to the list.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">ItemEventArgs</param>
        protected virtual void OnBeforeItemAdded(object sender, ItemEventArgs<T> e)
        {
            if (BeforeItemAdded != null)
            {
                BeforeItemAdded(sender, e);
            }
        }

        /// <summary>
        /// Raised when an Item is added to the list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">ItemEventArgs</param>
        protected virtual void OnItemAdded(object sender, ItemEventArgs<T> e)
        {
            if (ItemAdded != null)
            {
                ItemAdded(sender, e);
            }
        }

        /// <summary>
        /// Raised before a collection of Items is added to the list.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">ItemEventArgs</param>
        protected virtual void OnBeforeItemsAdded(object sender, ItemsEventArgs<T> e)
        {
            if (BeforeItemsAdded != null)
            {
                BeforeItemsAdded(sender, e);
            }
        }

        /// <summary>
        /// Raised when a collection of Items is added to the list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">ItemEventArgs</param>
        protected virtual void OnItemsAdded(object sender, ItemsEventArgs<T> e)
        {
            if (ItemsAdded != null)
            {
                ItemsAdded(sender, e);
            }
        }

        /// <summary>
        /// Raised before an Item is changed to the list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">GenericItemEventArgs</param>
        protected virtual void OnBeforeItemChanged(object sender, ItemEventArgs<T> e)
        {
            if (BeforeItemChanged != null)
            {
                BeforeItemChanged(sender, e);
            }
        }

        /// <summary>
        /// Raised when an Item is changed to the list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">ItemEventArgs</param>
        protected virtual void OnItemChanged(object sender, ItemEventArgs<T> e)
        {
            if (ItemChanged != null)
            {
                ItemChanged(sender, e);
            }
        }

        /// <summary>
        /// Raised before an Item is removed from the list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">ItemEventArgs</param>
        protected virtual void OnBeforeItemRemoved(object sender, ItemEventArgs<T> e)
        {
            if (BeforeItemRemoved != null)
            {
                BeforeItemRemoved(sender, e);
            }
        }

        /// <summary>
        /// Raised when an Item is removed from the list.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">ItemEventsArgs</param>
        protected virtual void OnItemRemoved(object sender, ItemEventArgs<T> e)
        {
            if (ItemRemoved != null)
            {
                ItemRemoved(sender, e);
            }
        }

        /// <summary>
        /// Raised when the Items are cleared from this list.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected virtual void OnItemsCleared(object sender, EventArgs e)
        {
            if (ItemsCleared != null)
            {
                ItemsCleared(sender, e);
            }
        }
        #endregion
    }

    public class ItemEventArgs<T> : EventArgs
    {
        /// <summary>
        /// Item
        /// </summary>
        public T Item { get; private set; }

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="Item"></param>
        public ItemEventArgs(T Item)
        {
            this.Item = Item;
        }
    }

    public class ItemsEventArgs<T> : EventArgs
    {
        /// <summary>
        /// Items
        /// </summary>
        public T[] Items { get; private set; }

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="Items"></param>
        public ItemsEventArgs(T[] Items)
        {
            this.Items = Items;
        }
    }
}

3 个答案:

答案 0 :(得分:2)

我可能弄错了。但你可以这样做,你的继承问题就不存在了。我将尝试举一个简单的例子:

你的集合类可能就是这样:

public class MyCollection
    {
        IList<string> MyList { get; set; }

        public event EventHandler<StringEventArgs> OnAdded;
        public event EventHandler<StringEventArgs> OnRemoved;

        public MyCollection()
        {
            MyList = new List<string>();
        }

        public void Add(string s)
        {
            MyList.Add(s);

            if (OnAdded != null)
                OnAdded(this, new StringEventArgs() { StringAddedOrRemoved = s });
        }

        public void Remove(string s)
        {
            MyList.Remove(s);
            if (OnRemoved != null)
                OnRemoved(this, new StringEventArgs() { StringAddedOrRemoved = s });
        }
    }

带有两个自定义EventHandler的非常简单的类:

public class StringEventArgs : EventArgs
    {
        public string StringAddedOrRemoved;


        public override string ToString()
        {
            return StringAddedOrRemoved;
        }
    }

这里没有什么难以理解的,然后基于这三种形式,您可以通过这种方式使用表单。

第一个按钮有两个按钮与集合进行交互,并创建两个将观察您的集合的表单:

public partial class Form1 : Form
    {
        public static MyCollection collection;


        public Form1()
        {

            InitializeComponent();

            collection = new MyCollection();

            Form2 form2 = new Form2();
            form2.Show();

            Form3 form3 = new Form3();
            form3.Show();

            collection.OnAdded += form2.MyCollectionAdded;
            collection.OnRemoved += form2.MyCollectionRemoved;

            collection.OnAdded += form3.MyCollectionAdded;
            collection.OnRemoved += form3.MyCollectionRemoved;

        }

        private void Add_Click(object sender, EventArgs e)
        {
            collection.Add("test add");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            collection.Remove("test add");
        }


    }

该集合将链接到将在此处观察到的表单的每个函数:

collection.OnAdded += form2.MyCollectionAdded;
                collection.OnRemoved += form2.MyCollectionRemoved;

                collection.OnAdded += form3.MyCollectionAdded;
                collection.OnRemoved += form3.MyCollectionRemoved;

所以我们需要实现这些形式:

public partial class Form2 : Form
    {


        public string Name { get; set; }
        public bool Flag { get; set; }

        public Form2()
        {
            InitializeComponent();
        }

        public void MyCollectionAdded(object sender, StringEventArgs e)
        {
            //Some action
            Flag = true;
            label1.Text = string.Format("{0} has added {1} to its list, flag={2}", Name, e.StringAddedOrRemoved, Flag);
        }

        public void MyCollectionRemoved(object sender, StringEventArgs e)
        {
            //Some action
            Flag = false;
            label1.Text = string.Format("{0} has removed {1} from its list, flag={2}", Name, e.StringAddedOrRemoved, Flag);
        }
    }

我从Form继承我的东西,但它可以从你想要的任何实际上继承。如果你想在不同的形式之间共享一些代码,可以考虑静态类中的辅助函数,或者任何可能适合你需要的模式。

希望它有所帮助,而且我并没有完全超出范围!

[编辑]哎呀没有看到编辑,对不起朋友![/编辑]

答案 1 :(得分:1)

如果它是&gt; = 3.5,您正在使用哪个.net版本,您可以使用ObservableCollection来实现您正在做的事情

答案 2 :(得分:1)

框架中有一个'ObservableCollection',仅用于此类事情。请参阅here