Passing an object to a view?

Topics: Prism v2 - WPF 3.5
Mar 26, 2009 at 7:07 AM
Hi,

I've got a simple bit of code to control my layout;

switch

 

(navigationAction.FakePageToLoad)

 

{

 

// Login

 

 

case "Login":

 

 

var pageLogin = Container.Resolve<MainPagesModule.Views.Login>();

 

region.Add(pageLogin);

region.Activate(pageLogin);

 

break;

 

 

// Logout

 

 

case "Logout":

 

 

var pageLogout = Container.Resolve<MainPagesModule.Views.Logout>();

 

region.Add(pageLogout);

region.Activate(pageLogout);

 

break;

 

 

default:

 

 

break;

 

}

If I wanted to pass some data to the logout view, how would I do that?

thanks in advance

george


Mar 26, 2009 at 9:30 PM

Hi George,

 

If I understand your scenario correctly, you want to be able to store specific data in your LogoutView after you got an instance of it. If this is the case, a possible approach could be using constructor injection, by getting as a parameter in your view the data object you require. In this way, your view has a constructor that resolves a dependency to this object and automatically injects the view with it. To be able to make this work, you first need to register the data object in the Unity container of your application.

 

In this article, from the Composite Application Guidance for WPF & Silverlight documentation, you can check how dependency injection is used to inject the presenter with its related view.

 

For more information on how to use the UnityContainer you can check the Container design concept and Design Patterns: Dependency Injection article.

 

Remember that this is one possible approach. If another approach suite your scenario better you should use it.

 

Please let me know if this helps.

 

Damian Schenkelman

http://blogs.southworks.net/dschenkelman