MEF - 如果我使用 MEF,如何创建新的 ViewModel(或重置其状态)?另外,如何避免内存问题?
基本上,我担心几件事:
[Export]
public class FooViewModel : NotificationObject
{
[Import]
public IService1 Service1 { get; set; }
[Import]
public IService2 Service2 { get; set; }
[Import]
public BarViewModel MyBar { get; set; }
etc.
//This is already initialized
public ObservableCollection<NotificationObject> CollectionOfViewModels { get; set; }
public void SetupViewModels1()
{
CollectionOfViewModels.Clear();
CollectionOfViewModels.Add(MyBar);
CollectionOfViewModels.Add(MyOtherBar);
CollectionOfViewModels.Add(My3rdViewModel);
etc.
}
public void SetupViewModels2()
{
CollectionOfViewModels.Clear();
CollectionOfViewModels.Add(MyBar);
CollectionOfViewModels.Add(My4thViewModel);
etc.
}
}
[Export]
public class BarViewModel : NotificationObject
{
[Import]
public IService1 Service1 { get; set; }
[Import]
public IService2 Service2 { get; set; }
//This is initialized and then the user starts adding values to it.
public ObservableCollection<Object> SomeObjects { get; set; }
public void DoSomething() { ... }
}
(1) 问题 #1,我加载我的 FooViewModel,一切都很好。我选择加载我的第一组 ViewModel 来使用它们进行一些工作。然后,当我去加载第二组 ViewModels 时,BarViewModel 仍然是同一个实例(因此它的所有属性都是相同的,包括 SomeObjects
属性。您如何告诉它重新初始化自身,以便所有它的数据被清除了吗?如果我这样做 MyBar = new MyBar()
那么 MyBar 不会初始化 IService1
或 IService2
(因为 MEF 没有 。
(2) 问题 #2,当我不使用其他 ViewModel 时,它们仍然驻留在内存中,因为我使用 MEF 导入它们,这意味着它们的状态也都在那里,这意味着很大 当你不再使用 ViewModel 时,如何完全摆脱它?
在我使用 MEF 之前,我会执行以下操作:
CollectionOfViewModels.Clear();
CollectionOfViewModels.Add(new MyBar());
etc.
这意味着当我进行下一组操作时一旦我执行CollectionOfViewModels.Clear();
,集合中的任何 ViewModel 就不再被引用,并且 GC 会收集它们。就目前而言,这样做实际上并没有摆脱任何 ViewModel。
Basically, I'm worried about a couple of things:
[Export]
public class FooViewModel : NotificationObject
{
[Import]
public IService1 Service1 { get; set; }
[Import]
public IService2 Service2 { get; set; }
[Import]
public BarViewModel MyBar { get; set; }
etc.
//This is already initialized
public ObservableCollection<NotificationObject> CollectionOfViewModels { get; set; }
public void SetupViewModels1()
{
CollectionOfViewModels.Clear();
CollectionOfViewModels.Add(MyBar);
CollectionOfViewModels.Add(MyOtherBar);
CollectionOfViewModels.Add(My3rdViewModel);
etc.
}
public void SetupViewModels2()
{
CollectionOfViewModels.Clear();
CollectionOfViewModels.Add(MyBar);
CollectionOfViewModels.Add(My4thViewModel);
etc.
}
}
[Export]
public class BarViewModel : NotificationObject
{
[Import]
public IService1 Service1 { get; set; }
[Import]
public IService2 Service2 { get; set; }
//This is initialized and then the user starts adding values to it.
public ObservableCollection<Object> SomeObjects { get; set; }
public void DoSomething() { ... }
}
(1) Issue #1, I load my FooViewModel and everything is nice and dandy. I choose to load my first set of ViewModels to do some work with them. Then when I go to load my second set of ViewModels BarViewModel is still the same instance (so all of it's properties are the same, including the SomeObjects
property. How do you tell it to reinitialize itself so all of it's data is cleared? If, instead I do MyBar = new MyBar()
then MyBar doesn't initialize IService1
or IService2
(because MEF didn't initialize it, I did).
(2) Issue #2, When I'm not using my other ViewModels they still reside in memory because I imported them with MEF, which means their states are all there as well, which means large large collections are sitting eating away at my memory. How do you fully get rid of a ViewModel when you aren't using it anymore?
See, before I was using MEF I would do the following:
CollectionOfViewModels.Clear();
CollectionOfViewModels.Add(new MyBar());
etc.
This would mean when I do my next set of ViewModels as soon as I did CollectionOfViewModels.Clear();
any of the ViewModels that were in the collection are no longer being referenced and the GC collects them. As it stands right now, doing this doesn't actually get rid of any of the ViewModels.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您熟悉 MEF 的
ExportFactory
功能吗?它允许您创建导出的新实例,并在完成后将其释放。这听起来像是您想要在这里做的事情。Are you familiar with MEF's
ExportFactory
functionality? It allows you to create a new instance of an export, as well as release it when you are done with it. That sounds like what you want to do here.