C# Caliburn Micro EventAggregator订阅/取消订阅同一ViewModel的多个实例
我最近发现我的Handle()方法在一次发布后被多次调用。。。发现停用和关闭的ViewModel实例实际上没有被处理,并且一直在接收消息!我正在寻找一种方法,可以正确地处理过时的ViewModel,也可以从EventAggregator订阅中删除它们 我的壳是一个贝壳C# Caliburn Micro EventAggregator订阅/取消订阅同一ViewModel的多个实例,c#,ioc-container,caliburn.micro,eventaggregator,C#,Ioc Container,Caliburn.micro,Eventaggregator,我最近发现我的Handle()方法在一次发布后被多次调用。。。发现停用和关闭的ViewModel实例实际上没有被处理,并且一直在接收消息!我正在寻找一种方法,可以正确地处理过时的ViewModel,也可以从EventAggregator订阅中删除它们 我的壳是一个贝壳 Conductor<T>.Collection.OneActive, IScreenEx where T : class, IScreenEx 最后,基类构造函数是: public BaseConductor
Conductor<T>.Collection.OneActive, IScreenEx where T : class, IScreenEx
最后,基类构造函数是:
public BaseConductor(SimpleContainer container, IEventAggregator eventAggregator, IDialogManager dialogs)
{
this.container = container;
this.eventAggregator = eventAggregator;
this.eventAggregator.Subscribe(this);
this.Dialogs = dialogs;
}
请帮助。在
IEventAggregator
界面上有一个Unsubscribe
方法
关于何时调用它的问题的答案取决于您的体系结构-您发布的代码无法解释何时不再需要视图模型-它们可能永远留在项目中
通常你会在激活中订阅OnActivate
并在OnDeactivate
中取消订阅,但是你的代码是在构造函数中订阅的,所以我猜你可能希望订阅非活动模型,所以CanClose
可能是另一个候选
public X SwitchToNamed<X>(String key) where X : T
{
var existing = Items.FirstOrDefault(y => y.GetType() == typeof(X) && y.UniqueKey == key);
if (existing == null)
existing = Items.FirstOrDefault(x => x.GetType().GetInterfaces().Contains(typeof(X)) && x.UniqueKey == key);
if (existing != null)
{
ChangeActiveItem(existing, false);
return (X)existing;
}
else
{
X vm = container.GetInstance<X>();
vm.UniqueKey = key;
ActivateItem(vm);
return vm;
}
}
public LinkageGraphViewModel
(
Caliburn.Micro.SimpleContainer container,
Caliburn.Micro.IEventAggregator eventAggregator,
IDialogManager dialogs,
IDataService dataService,
IReferentialData refData
)
: base(container, eventAggregator, dialogs, dataService, refData)
{
DisplayName = Strings.Movements;
}
public BaseConductor(SimpleContainer container, IEventAggregator eventAggregator, IDialogManager dialogs)
{
this.container = container;
this.eventAggregator = eventAggregator;
this.eventAggregator.Subscribe(this);
this.Dialogs = dialogs;
}