2013-02-19 80 views
0

我为我的观点设置了PartCreationPolicy.NonShared,但对于某些特定的用户,我可以使用CreationPolicy.Shared(为了提高性能),我不确定是否可以完成。因为我使用ServiceLocator来获取Views的实例。像PRISM零件制作政策非共享/与MEF共享

view = ServiceLocator.GetInstance<MyUserControl>("view_contract");

可能是什么造成这种情况的最佳解决方案。我已经在谷歌搜索它,我发现了一些解决方案与CompositionContainer.GetExports,但

1-我无法得到CompositionContainer实例在我的ViewModel。

2- In This Post,它下GetExport写了

出口价值的连续调用将返回相同的 情况下,不管是否已经共享或非共享 寿命。

请任何人都可以提出最佳解决方案和一些示例代码片段吗?

+0

看一看ExportFactory (http://blogs.msdn.com/b/bclteam/archive/2011/11/17/exportfactory-amp-lt-t-amp-gt-in-mef-2-alok.aspx)。而不是GetExport返回一个懒惰使用ExportFactory 。 – 2013-02-19 15:04:20

+0

谢谢@PanosRontogiannis,但是我如何在ViewModel中获得'CompositionContainer'?如第1点? – 2013-02-20 06:27:41

回答

2

据我所知,你有一些“商业逻辑”来区分共享和非共享视图(例如根据用户类型)。我认为这不应该在你的DI容器中处理...

如果你想实现这个与“棱镜式”,那么你可以在您的ViewModels中使用棱镜INavigationAware接口:视图和viewModel是non-共享,并通过导航激活/构建它(与MEF完美配合)。将“共享”/“非共享”的业务逻辑放入“IsNavigationTarget”方法中。 Prism将自动调用此方法,并仅在需要时才创建新的视图实例。

下面是一些代码:(!不要忘记视图名称作为导航目标)

的观点:

[Export(Constants.ViewNames.MyFirstViewName)] // The Export Name is only needed for Prism's view navigation. 
[PartCreationPolicy(CreationPolicy.NonShared)] // there may be multiple instances of the view => NO singleton!! 
public partial class MyFirstView 
{ ... } 

视图模型:

[PartCreationPolicy(CreationPolicy.NonShared)] 
    [Export] 
    public class MyFirstViewModel: Microsoft.Practices.Prism.Regions.INavigationAware 
    { 
     #region IINavigationAware 
     // this interface ships with Prism4 and is used here because there may be multiple instances of the view 
     // and all this instances can be 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) 
     { 
      // use any kind of busines logic to find out if you need a new view instance or the existing one. You can also find any specific target view using the navigationContext... 
      bool thereCanBeOnlyOneInstance = ... 
      return thereCanBeOnlyOneInstance; 
     }  

     #endregion 
} 
+0

请分享一些代码示例? 'IsNavigationTarget'上需要做些什么 – 2013-02-20 12:59:25

+0

我在上面添加了示例代码。欢呼, – Sebastian 2013-02-20 13:19:39

+0

我正在使用'IRegionManager'为'Region'添加一个视图,并使用'ServiceLocator.GetInstance ()'来获取实例来查看,'Service Locator'使用'PartCreationPolicy'获取实例,我怎么能从上面的代码控制这种行为? – 2013-02-20 13:55:00