Hi I have created a simple Main application and two InjectedApplications (InjectedApplication1 and InjectedApplication2) using an aritcle Simplified MEF: Dynamically Loading a Silverlight .xap.
What i want to do then is how do we pass a value from Main Application to Injected application?
Here is the scenario: I have Main Application in which i Inject xap on demand. Untill here everything works fine as mentioned in the above article. I need to extend few things from there:
The xap (injected one) should have the code to load values for some particuilar ID. so when i load this xap in the Main application(Where the injected xap is loaded) i want to pass a value(some ID) to injected XAP so that the xap will load information for that particuilar ID.
How can we achieve this??
You should have
Exportattributes on the types on the injected application. In those attribute, you can define an interface. Have the exported type implement that same interface. When you construct objects using MEF, you will have access to the exported interface. You can use that to pass data to the constructed object.I recommend defining the interfaces in a separate shared library project.
If you can’t or don’t want to do that, you could use an Event Aggrigator like as MVVM Light’s Messenger. The Messenger has a static
Defaultproperty. I’m pretty sure the two xap files will share the same Default Messenger. You can then send and receive data that way.Example
Shared Lib
Injected App
Main App
Now when you call
CompositionInitializer.SatisfyImports(this)onBar,MyFoowill be set to an instance ofFoofrom Injected App. Because this implementsIFoo,you are able to use this interface to interact with the classFoo.If you need any more clarification, I’ll need to see your code.