WPFKK
WPFKK

Reputation: 1477

While Dynamically loading xap using MEF how to pass a parameter to the xap file?

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??

Upvotes: 1

Views: 576

Answers (1)

cadrell0
cadrell0

Reputation: 17327

You should have Export attributes 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 Default property. 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

public interface IFoo
{
    object Data { get; set; }
}

Injected App

[Export(typeof(IFoo))]
public class Foo : IFoo
{
    public object Data { get; set; }
}

Main App

public class Bar
{
    [Import]
    public IFoo MyFoo { get; set; }
}

Now when you call CompositionInitializer.SatisfyImports(this) on Bar, MyFoo will be set to an instance of Foo from Injected App. Because this implements IFoo, you are able to use this interface to interact with the class Foo.

If you need any more clarification, I'll need to see your code.

Upvotes: 2

Related Questions