2013-02-19 35 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