1, first in the project reference to add: System.ComponentModel.Composition and Bbv.Common.EventBroker
Where Bbv.Common.EventBroker referenced DLL library in http://sourceforge.net/projects/bbvcommon/ can be downloaded, if not get, can leave a message with me, I provide to everyone.
:
2, the specific Code implementation 1), the main form code:
Public partial class Frmmain:form {private void Initmef () {Aggregatecatalog aggregatecata Log = new Aggregatecatalog ();//Declares a collection Assemblycatalog Assemblycatalog = new Assemblycatalog (Assembly.getexecuti Ngassembly ());//declares the current application's catalog aggregateCatalog.Catalogs.Add (assemblycatalog);//Adds the current application's catalog to the collection Compositioncontainer Compositioncontainer = new Compositioncontainer (aggregatecatalog);//Declare a container compositio Ncontainer.composeparts (this);//Create Component} [Import ("user")] private usercontroltest usercontr oltest {set; get;} Public Frmmain () {InitializeComponent (); INITMEF ();//Initialize MEF Panel1. Controls.clear (); Panel1. Controls.Add (usercontroltest); Eventbroker eventbroker = new Eventbroker (); Eventbroker.register (usercontroltest); Eventbroker.register (this); } [EventsubscriptIon (Eventtopics.firstevent, typeof (Background))] public void Receiver (object obj, EventArgs args) { System.Threading.Thread.Sleep (3000); MessageBox.Show ("received the message"); The private void Timer1_Tick (object sender, EventArgs e) {Label1. Text = DateTime.Now.ToString ("hh:mm:ss.fff"); } }
2) UserControl Code:
[Export ("User")] public partial class Usercontroltest:usercontrol {public usercontroltest () { InitializeComponent ( ); } [Eventpublication (eventtopics.firstevent)] public event EventHandler TestEvent; private void btnTest_Click (object sender, EventArgs e) { testevent (this,eventargs.empty); System.Threading.Thread.Sleep (+); } }
3) EventTopics.cs Code:
public class Eventtopics {public const string firstevent = "Test"; }
C # uses MEF to dynamically load different UserControl and implement communication between different UserControl