Saturday, 15 September 2012

wpf - PRISM Part Creation Policy NonShared/Shared with MEF -



wpf - PRISM Part Creation Policy NonShared/Shared with MEF -

i have set partcreationpolicy.nonshared view, specific users can utilize creationpolicy.shared (for performance improvement), not sure if can done or not. using servicelocator instances of views.

view = servicelocator.getinstance<myusercontrol>("view_contract");

what best solution this. have searched on google it, have found solutions compositioncontainer.getexports,

1- unable compositioncontainer instance in viewmodel.

2- in post , written under getexport

successive invocation of export's value homecoming same instance, regardless of whether part has shared or non-shared lifetime.

please 1 suggest best solution , illustration code snippet it?

as understood, have "business logic" distinguish between shared , non-shared views (e.g. depending on type of users). think should not handled in di container...

if want achief "prism-style" utilize prism inavigationaware interface in viewmodels: view , viewmodel non-shared , activate/construct via navigation (works mef). set business logic "shared"/"non-shared" "isnavigationtarget" method. prism phone call method automatically , create new view instance if needed.

here code:

the view (don't forget view name navigation target!):

[export(constants.viewnames.myfirstviewname)] // export name needed prism's view navigation. [partcreationpolicy(creationpolicy.nonshared)] // there may multiple instances of view => no singleton!! public partial class myfirstview { ... }

the viewmodel:

[partcreationpolicy(creationpolicy.nonshared)] [export] public class myfirstviewmodel: microsoft.practices.prism.regions.inavigationaware { #region iinavigationaware // interface ships prism4 , used here because there may multiple instances of view // , instances can targets of navigation. /// <summary> /// called when [navigated to]. /// </summary> /// <param name="navigationcontext">the navigation context.</param> public override void onnavigatedto(navigationcontext navigationcontext) { ... } /// <summary> /// </summary> public override void onactivate() { ... } /// <summary> /// determines whether [is navigation target] [the specified navigation context]. /// </summary> /// <param name="navigationcontext">the navigation context.</param> /// <returns> /// <c>true</c> if [is navigation target] [the specified navigation context]; otherwise, <c>false</c>. /// </returns> public override bool isnavigationtarget(navigationcontext navigationcontext) { // utilize kind of busines logic find out if need new view instance or existing one. can find specific target view using navigationcontext... bool therecanbeonlyoneinstance = ... homecoming therecanbeonlyoneinstance; } #endregion }

wpf prism mef containers service-locator

No comments:

Post a Comment