C # uses MEF to dynamically load different UserControl and implement communication between different UserControl

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.