c# - How to retrieve/import object using MEF in Prism -


I need an object ( RuleFile ) for the whole application (* .docs) The file that will be attached to my application.

I am using Prism 5 with MEF as a dependency injection container. [Export] [Serializable (public)] Public Class Rule File: NotificationBase, IreneFile {}

I now decorate the object with [export]] < / Code> and trying to import it into one of MyViewModel but it is giving null .

  Public class MyViewModel: ViewModelBase {[Import] Private rulesfile rulefile; // 'tap' is coming here)  

Please tell me what am I missing? Or tell me another way to handle this scenario better.

Are you checking the value in the constructor? Directly decorated on the property are resolved after the constructor. If you want to use the RuleFile in the constructor, you must set this way

  public class MyViewModel: ViewModelBase {public Niamfail RuleFile {get; Set; } [Importer resource] Public MyViewModel (RuleFile ruleFile) {RuleFile = ruleFile; }}  

Alternatively, you can implement IPartImportsSatisfiedNotification , which will give you an information method to inform that the import has been resolved . This way

  public class MyViewModel: ViewModelBase, IPartImportsSetisfiedNotification {[Import] Public Rule File RuleFile {get; Set; } Public Zero OnImportsSatisfied () {// This indicates that the import has been solved}}  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -