Plugins in WPF MvvM with MEF 2

admin

Administrator
Staff member
I have tried the following very good tutorial <a href="https://www.eidias.com/blog/2013/7/26/plugins-in-wpf-mvvm-with-mef#cm-249" rel="nofollow noreferrer">https://www.eidias.com/blog/2013/7/26/plugins-in-wpf-mvvm-with-mef#cm-249</a> to migrate to MEF2 but for some reason the assemblies are not shown in the catalog. From MEF2 I wanted to use the API Configuration (RegistrationBuilder class) (here an example: <a href="https://stefanhenneken.wordpress.com/2013/01/21/mef-teil-11-neuerungen-unter-net-4-5/" rel="nofollow noreferrer">https://stefanhenneken.wordpress.com/2013/01/21/mef-teil-11-neuerungen-unter-net-4-5/</a> ), maybe somebody has an idea how to apply MEF2 correctly to the tutorial. Thank you very much.

here the overview of the solution:
<a href=" " rel="nofollow noreferrer"><img src=" " alt="solution overview"></a>

In the MainViewModel.cs I don't know yet how to integrate the imports into the
Code:
RegistrationBuilder
.Can you check the rest of the code? Thanks.

Code:
namespace WPF_MEF_App
{
    public class MainWindowModel : NotifyModelBase
    {
        public ICommand ImportPluginCommand { get; protected set; }
        private IView PluginViewVar;

       [Import(typeof(IView), AllowRecomposition = true, AllowDefault = true)]
        public IView PluginView
        {
            get { return PluginViewVar; }
            set{ PluginViewVar = value; NotifyChangedThis();}
        }

        [ImportMany(typeof(IView), AllowRecomposition = true)]
        public IEnumerable&lt;Lazy&lt;IView&gt;&gt; Plugins;

        private AggregateCatalog catalog;
        private CompositionContainer container;

        public MainWindowModel()
        {
            ImportPluginCommand = new DelegateCommand(ImportPluginExecute);
            RegistrationBuilder builder = new RegistrationBuilder();
            builder.ForType&lt;PluginSecondScreen&gt;()
                .Export&lt;IView&gt;(eb =&gt;
                {
                    eb.AddMetadata("Name", "PluginSecond");
                })
                .SetCreationPolicy(CreationPolicy.Any);
            //.ImportProperties(pi =&gt; pi.Name == "IView",
            //        (pi, ib) =&gt; ib.AllowRecomposition());

            builder.ForType&lt;CalculatorScreen&gt;()
                .Export&lt;IView&gt;(eb =&gt;
                {
                    eb.AddMetadata("Name", "CalculatorScreen");
                })
                .SetCreationPolicy(CreationPolicy.Any);
                //.ImportProperties(pi =&gt; pi.Name == "IView",
                //        (pi, ib) =&gt; ib.AllowRecomposition());

            catalog = new AggregateCatalog();

            string pluginsPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            catalog.Catalogs.Add(new DirectoryCatalog(pluginsPath, "Plugin*.dll"));
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly(), builder));

            //also we add to a search path a subdirectory plugins
            pluginsPath = Path.Combine(pluginsPath, "plugins");
            if (!Directory.Exists(pluginsPath))
                Directory.CreateDirectory(pluginsPath);
            catalog.Catalogs.Add(new DirectoryCatalog(pluginsPath, "Plugin*.dll"));            

            //Create the CompositionContainer with the parts in the catalog.
            container = new CompositionContainer(catalog);
        }

        private void ImportPluginExecute()
        {
            //refresh catalog for any changes in plugins
            //catalog.Refresh();

            //Fill the imports of this object
            //finds imports and fills in all preperties decorated
            //with Import attribute in this instance
            container.ComposeParts(this);
            //another option
            //container.SatisfyImportsOnce(this);
        }
    }
}

Here are the two plugins:
I have already commented the exports here, because they are no longer needed for
Code:
RegistrationBuilder
.
<a href=" " rel="nofollow noreferrer"><img src=" " alt="enter image description here"></a>
<a href=" " rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/taR69.png" alt="enter image description here"></a>