Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 我可以重新创建CompositionContainer吗?_Wpf_Mef - Fatal编程技术网

Wpf 我可以重新创建CompositionContainer吗?

Wpf 我可以重新创建CompositionContainer吗?,wpf,mef,Wpf,Mef,在显示主窗口之前,我有一个带有登录窗口的wpf应用程序 我使用mef加载所有模块/部件。在主窗口开始之前,我检查用户登录数据与我随后显示的部分。共享和非共享的部分 [ImportMany] private IEnumerable<Lazy<IComponent, IComponentMetadata>> _components; [ImportMany("Resourcen", typeof(ResourceDictionary))] private IEnumerab

在显示主窗口之前,我有一个带有登录窗口的wpf应用程序

我使用mef加载所有模块/部件。在主窗口开始之前,我检查用户登录数据与我随后显示的部分。共享和非共享的部分

[ImportMany]
private IEnumerable<Lazy<IComponent, IComponentMetadata>> _components;

[ImportMany("Resourcen", typeof(ResourceDictionary))]
private IEnumerable<ResourceDictionary> _importResourcen;

var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory));
_mefcontainer = new CompositionContainer(catalog);

_mefcontainer.ComposeParts(somepartwithaSharedExport, this);
首先,我认为它是有效的,但似乎我第一次创造的部分仍然存在于记忆中,我没有机会杀死它们。所以,当我重新登录足够多次时,我就摆脱了内存异常

这就是我现在使用这种方法的原因

  System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
  App.ShutDown();
我对此感到不高兴


有没有办法清理Compositioncontainer并创建一个新的容器?

您可以尝试调用\u mefcontainer.removePartSomePartWithAredExport。此处有更多详细信息:

对于非共享部分,您可以调用:

有关更多信息,请尝试此示例中的示例代码


据我所知,如果不处理容器,共享部件就无法释放。如果使用该路径,那么还必须确保没有保留对这些对象的引用,以允许GC收集它们。mrtig答案中的文档参考提供了许多关于零件寿命的有用细节,您可能应该与weshaggard的类似问题一起研究。它还解释了一次性零件会发生什么情况。

从外观上看,您可能不需要为了循环登录而转储导入。你能解释一下你为什么需要重新作曲吗?
  System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
  App.ShutDown();
_mefcontainer.ReleaseExport(nonSharedExport);