开发者

Prism / MEF new view not getting a new viewmodel from MEF import

I have a tabbed application where I want the user to be able to search for a person and then, in a new view, show the person's details. The user should be able to have multiple person detail views open for different people.

I was a little unsure if I am following the correct procedure for creating my new view. Using Unity (which I am not) it seems you would call Container.Resolve(view) however I am doing the following, the satisfyImports was necessary in order for my imports in the view / viewmodel to be created.

PersonDetailView view = new PersonDetailView();
_container.Satisfy开发者_开发技巧ImportsOnce(view);
_regionManager.Regions["MainRegion"].Add(view, this.SelectedPerson.Name);
_regionManager.RequestNavigate("MainRegion", new Uri("PersonDetailView", UriKind.Relative));

In the code for my PersonDetailView I have the following property to set the data context.

[Import]
public PersonDetailsViewModel ViewModel
{
   set
   {
       this.DataContext = value;
   }
} 

This seems to work but the trouble I am having is that when I create a second person view, the new view is getting the same instance of the datacontext as the view that is already created.

Is this because I am creating my views incorrectly or is there a way that I tell MEF to create a new oject when it fulfills the imports for my new view?


When you export a part, by default it used a CreationPolicy of Shared. This essentially makes the exported instance a singleton in the container. With your export, add another attribute:

[Export, PartCreationPolicy(CreationPolicy.NonShared)]
public class Foo { }

This will ensure a new instance is created each time you call to compose the consumer instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜