【问题标题】:PRISM Part Creation Policy NonShared/Shared with MEFPRISM 零件创建政策非共享/与 MEF 共享
【发布时间】:2013-02-03 19:59:52
【问题描述】:

我已经为我的视图设置了PartCreationPolicy.NonShared,但是对于某些特定的用户我可以使用CreationPolicy.Shared(为了提高性能),我不确定是否可以这样做。因为我使用ServiceLocator 来获取视图实例。喜欢

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

什么是最好的解决方案。我在谷歌上搜索过,我找到了CompositionContainer.GetExports的一些解决方案,但是

1- 我无法在我的 ViewModel 中获取 CompositionContainer 实例。

2-In This PostGetExport下写的是

连续调用导出的值将返回相同的值 实例,无论零件是否具有共享或非共享 终生。

请任何人提出最佳解决方案和一些示例代码 sn-p 吗?

【问题讨论】:

  • 看看 ExportFactory (blogs.msdn.com/b/bclteam/archive/2011/11/17/…)。而不是返回 Lazy 的 GetExport 使用 ExportFactory.
  • 谢谢@PanosRontogiannis,但我怎样才能在我的视图模型中获得CompositionContainer?如第 1 点所述?

标签: wpf prism mef containers service-locator


【解决方案1】:

据我了解,您有一些“业务逻辑”来区分共享视图和非共享视图(例如,取决于用户的类型)。我认为这不应该在您的 DI 容器中处理...

如果您想通过“prism-style”实现这一点,那么您可以在 ViewModel 中使用 prism INavigationAware 接口:视图和 viewModel 是非共享的,您可以通过导航激活/构建它(与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
}

【讨论】:

  • 能否分享一些代码示例? IsNavigationTarget需要做什么
  • 我使用IRegionManagerRegion添加视图,使用ServiceLocator.GetInstance&lt;T&gt;()获取实例查看,Service Locator获取使用PartCreationPolicy的实例,我该如何控制上述代码中的这种行为?
  • RegionManager 将为您完成这项工作。只需调用“RequestNavigate”,您就不必自己查找视图/视图模型实例:IRegionManager regionManager = ...; regionManager.RequestNavigate("MainRegion", new Uri("InboxView", UriKind.Relative));在此处查看MSDN Doc...
  • 但它会使用PartCreationPolicy 属性,在这种情况下我不想要它。
猜你喜欢
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
相关资源
最近更新 更多