Fa

Oct 29, 2009 at 9:16 PM
Edited Oct 30, 2009 at 8:15 AM

Hi

I have the following declaration:

[PartCreationPolicy(CreationPolicy.NonShared)]Export(typeof(IContent)), ShellModuleMetadata(......)]
public partial class UserControl1 : UserControl, IContent
{  ....  }

Then I have my Import:

 

[

if I do:

ImportMany(RequiredCreationPolicy = CreationPolicy.NonShared)]
public PartCreator<IContent, IShellModuleMetadata>[] factory { get; set; }

var item1 = factory.CreatePart().ExportedValue;
var item2 = factory.CreatePart().ExportedValue;

I had expected that item1 and item2 differs,,,  but it seems like they are the same instance.

Can anyone explaine why they are the same and not different instances. Is it possoble to have different instances???

I am using Beta 2 with a addon according to this blog to make it work http://blogs.msdn.com/nblumhardt/archive/2009/08/28/dynamic-part-instantiation-in-mef.aspx

//regards
//lasse

 

 

 

Developer
Oct 29, 2009 at 10:24 PM

I agree that item1 and item2 should be different, in my quick testing it does appear to produce different instances. However I want to first understand the code snippets you showed. According to your code factory it is an array so you would need to do factory[...].CreatePart().ExportedValue, and how are you determining that item1 and item2 are the same instance?

Oct 30, 2009 at 8:55 AM

Hi again

My application are unfourtnatly a bit more complex than shown in my example. To give more information,,,

I have a custom control based on a WPF MenuItem.

<font size="2">

[

</font>

PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(IShellButton))]
public class ShellButton : MenuItem, IShellButton
{
   
public PartLifetimeContext<IContent> Creator { get; set; }

.....

}

I create a instance of ShellButton by:

<font size="2">

[

</font>

Import] PartCreator<IShellButton> ModuleButtonCreator;
....
....<font size="2">

var newButton = ModuleButtonCreator.CreatePart().ExportedValue; 
newButton.Creator = factory[i];

</font>

 

.....
.....

Now Im back to the original code exept that:
var item1 = factory.CreatePart().ExportedValue;
var item2 = factory.CreatePart().ExportedValue;

should be:
var item1 = Creator.CreatePart().ExportedValue;
var item2 = Creator.CreatePart().ExportedValue;

 

When I try to add item1 and item2 to a TabControl,, I get a exception,, 
{"Specified element is already the logical child of another element. Disconnect it first."}

Maby this is a weakness....

//lasse

 

 

 

 

Developer
Oct 30, 2009 at 4:35 PM

public PartLifetimeContext<IContent> Creator { get; set; }

Is that the Creator you are storing? If so that isn't actually a creator that is actually an instance. You need to store PartCreator<IContext> not PartLifetimeContext<IContent>.

From that I would assume that your sample isn't actually:

var item1 = Creator.CreatePart().ExportedValue;
var item2 = Creator.CreatePart().ExportedValue;

but
var item1 = Creator.ExportedValue;
var item2 = Creator.ExportedValue;

in which case item1 and item2 will be the same.

Oct 31, 2009 at 4:30 PM

That did it......  Now it work as I had hoped.

If you ever are in Stockholm,,, I owe you a beer.

//lasse