Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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
C# Caliburn Micro EventAggregator订阅/取消订阅同一ViewModel的多个实例_C#_Ioc Container_Caliburn.micro_Eventaggregator - Fatal编程技术网

C# Caliburn Micro EventAggregator订阅/取消订阅同一ViewModel的多个实例

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

我最近发现我的Handle()方法在一次发布后被多次调用。。。发现停用和关闭的ViewModel实例实际上没有被处理,并且一直在接收消息!我正在寻找一种方法,可以正确地处理过时的ViewModel,也可以从EventAggregator订阅中删除它们

我的壳是一个贝壳

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;
    }