c# - Load a XAML file in a ResourceDictionary from a ClassLibrary -


i'm trying make simple plugin system in plugins dynamically loaded .dll files on application's startup , show in ui.

this answer seems i'm looking for. uses mef load plugins. tried create simple project , follow instructions. solution has following structure:

  • mefttest (contains main meftest.exe, references meftest.sdk , not plugins)
  • meftest.sdk (contains iplugin.cs , ipluginviewmodel.cs , engine.cs loads plugins application's directory)
  • meftest.plugin1 (contains first plugin, references meftest.sdk)
  • meftest.plugin2 (contains second plugin, references meftest.sdk)

meftest.sdk -> iplugin.cs

public interface iplugin {     ipluginviewmodel viewmodel { get; }     resourcedictionary view { get; }     string title { get; } } 

meftest.sdk -> engine.cs

public class engine {     [importmany]     private ienumerable<iplugin> plugins { get; set; }      public async task<observablecollection<iplugin>> getplugins()     {         try         {             var folder = appdomain.currentdomain.basedirectory;             var catalog = new aggregatecatalog();             //catalog.catalogs.add(new assemblycatalog(assembly.getexecutingassembly()));             catalog.catalogs.add(new directorycatalog(folder));             var container = new compositioncontainer(catalog);             container.composeparts(this);              var result = new observablecollection<iplugin>();              foreach (var p in plugins)             {                 result.add(p);             }              return result;         }         catch (exception ex)         {             //i exception here...             var t = ex;             throw;         }     } } 

meftest.plugin1 -> plugin1.cs

[export(typeof(iplugin))] public class plugin1 : iplugin {     private mainviewmodel viewmodel { get; set; }      public ipluginviewmodel viewmodel     {         { return viewmodel; }     }      public resourcedictionary view     {         { return viewdictionary; }     }      private resourcedictionary viewdictionary = new resourcedictionary();      public string title     {         { return "plugin 1"; }     }      [importingconstructor]     public plugin1()     {         //i error here. tried both of these, none of them work         viewdictionary.source =         //    new uri("pack://application:,,,/meftest.plugin1;component/views/main.xaml", urikind.absolute);         new uri("/meftest.plugin1;component/views/main.xaml",          urikind.relative);     }      public override string tostring()     {         return title;     } } 

however, error could not load file or assembly 'meftest.plugin1.dll, culture=neutral' or 1 of dependencies. system cannot find file specified.

the main.xaml file in meftest.plugin1\views\main.xaml folder. output type of project classlibrary , build action of xaml file page.

ps: tried reference plugin directly , add without mef (plugins.add(new plugin3.plugin3());) , still threw same exception. don't think problem mef part of solution.

how can fix this? also, there better option approach?

i way in xbap application...to retreive distant xaml. try same local resourtce

private resourcedictionary loaddictionary(string source)     {         stream streaminfo = null;         resourcedictionary dictionary = null;          try         {             streaminfo = distantmanager.instance.getresource(source);              if (streaminfo != null)             {                 uri baseuri = distantmanager.instance.geturi(source);                  dictionary = xamlreader.load(streaminfo) resourcedictionary;                  dictionary.source = baseuri;             }         }         catch (exception e)         {             businesslogger.manage(e);             return null;         }          return dictionary;     } 

something

uri baseuri = new uri(mysource);                 dictionary = xamlreader.load(xxxx) resourcedictionary;                 dictionary.source = baseuri; 

but on other hand not understand why want resourcedictionary plugin view...? create plugin user control ??


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -