Note that there are some explanatory texts on larger screens.

plurals
  1. POMEF. How to load a winform into winform container?
    primarykey
    data
    text
    <p>I have decided to play a little bit with MEF2 and net3.5 and I have thought it would be easy but I am stuck now. Generally the idea of my toy is I want to have form containet where I am going to load form extensions and show them. I did this code</p> <p>My extension:</p> <pre> <code> using System.ComponentModel.Composition; using System.Windows.Forms; namespace MyExtantion { public interface IForm { void LoadForm(Form form); } [Export(typeof(IForm))] public partial class MyExtantion : Form, IForm { public MyExtantion() { InitializeComponent(); } public void LoadForm(Form form) { MdiParent = form; Show(); } } } </code> </pre> <p>and form container </p> <pre> <code> using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection; using System.Windows.Forms; namespace FormsContainer { public partial class FormContainer : Form { public FormContainer() { InitializeComponent(); } private CompositionContainer _container; public interface IForm { void LoadForm(Form form); } [Import(typeof(IForm))] public IEnumerable Forms { get; set; } private bool Compose() { var catalog = new AggregateCatalog( new AssemblyCatalog(Assembly.GetExecutingAssembly()), new DirectoryCatalog("Extantions")); var batch = new CompositionBatch(); batch.AddPart(this); _container = new CompositionContainer(catalog); try { _container.Compose(batch); } catch (CompositionException compositionException) { MessageBox.Show(compositionException.ToString()); return false; } return true; } private void FormContainer_Load(object sender, EventArgs e) { if (Compose()) foreach (IForm form in Forms) { form.LoadForm(this); } } } } </code> </pre> <p>The problem is I can not load my extantion and I have this error</p> <blockquote> <p>{"The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.\r\n\r\n1) No exports were found that match the constraint '((exportDefinition.ContractName = \"FormsContainer.FormContainer+IForm\") &amp;&amp; (exportDefinition.Metadata.ContainsKey(\"ExportTypeIdentity\") &amp;&amp; \"FormsContainer.FormContainer+IForm\".Equals(exportDefinition.Metadata.get_Item(\"ExportTypeIdentity\"))))'.\r\n\r\nResulting in: Cannot set import 'FormsContainer.FormContainer.Forms (ContractName=\"FormsContainer.FormContainer+IForm\")' on part 'FormsContainer.FormContainer'.\r\nElement: FormsContainer.FormContainer.Forms (ContractName=\"FormsContainer.FormContainer+IForm\") --> FormsContainer.FormContainer\r\n"}</p> </blockquote> <p>How I can achieve it with MEF? and What I do wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload