请不要仅仅为了将项目添加到列表中而从另一个表单中引用一个表单。:)
根据上述 Rex 的回答,您可以实现域事件模式(http://www.martinfowler.com/eaaDev/DomainEvent.html)
一个简单的(基本的)实现将有一个管理事件/触发事件的单例类:
using System;
/// <summary>
/// Class representing a single source for domain events within an application.
/// </summary>
public class DomainEventSource
{
#region Fields
private static readonly Lazy<DomainEventSource> source = new Lazy<DomainEventSource>( () => new DomainEventSource() );
#endregion
#region Properties
/// <summary>
/// Gets a reference to the singleton instance of the <see cref="DopmainEventSource"/> class.
/// </summary>
/// <value> A<see cref="DomainEventSource"/> object.</value>
public static DomainEventSource Instance
{
get
{
return source.Value;
}
}
#endregion
#region Methods
/// <summary>
/// Method called to indicate an event should be triggered with a given item name.
/// </summary>
/// <param name="name">A <see cref="string"/> value.</param>
public void FireEvent( string name )
{
if ( this.AddItem != null )
{
this.AddItem( source, new AddItemEvent( name ) );
}
}
#endregion
#region Events
/// <summary>
/// Event raised when add item is needed.
/// </summary>
public EventHandler<AddItemEvent> AddItem;
#endregion
}
然后连接并调用事件,例如:
DomainEventSource.Instance.AddItem += ( s, a ) => Console.WriteLine( "Event fired with name: " + a.ItemName );
DomainEventSource.Instance.FireEvent("事物");
有了这个,你必须记住事件是内存泄漏的一个简单来源。如果您注册它,请确保您取消注册它。