【问题标题】:Caliburn Micro Screen Activated EventsCaliburn 微屏激活事件
【发布时间】:2016-09-01 08:18:44
【问题描述】:

我在我的项目中使用 Caliburn Micro 和 AvalonDock。我尝试在屏幕被激活时处理事件。我的主视图模型是“Conductor.Collection.OneActive”,每个选项卡“文档”都是“屏幕”。 我的主视图模型中有一个函数,如下所示:

public void CheckAndRegisterDocument(DocumentViewModel document)
    {
        DocumentViewModel exists = _documents.FirstOrDefault((d) => { return d.Equals(document); });
        // if not exists - add it 
        if(exists == null) {
            document.Activated += Document_Activated;
            _documents.Add(document);
            Items.Add(document);
        }
        // activate and set property object
        ActivateItem(document);

        Properties.PropertiesObject = document.Properties;
    }

    // document activated event handler
    private void Document_Activated(object sender, ActivationEventArgs e) {
        ActiveDocument = sender as DocumentViewModel;
    }

但未调用“Document_Activated”函数。我做错了什么?

【问题讨论】:

  • 您确定不是连续第二次尝试激活同一个屏幕吗?
  • 请注意。无需手动将文档添加到Items 集合中,它会在您调用ActivateItem(document) 时添加。似乎_documentsActiveDocument 只是在复制内置的导体属性:ItemsActiveItem
  • 感谢您的回复。是的,_documentsItems 看起来一样,它们是 )...这是我的任何实验的结果。我正在寻找决定。我不明白为什么它不起作用。何时应该调用 Activated 事件?...而且,是的,我不会尝试第二次激活同一个屏幕。
  • 我无法使用 Caliburn.Micro 3.0.1.0 重现此问题。我制作了简单的“DocumentViewModel”类,它派生自“Screen”类,一旦我调用ActivateItem(document),在派生自Conductor<Screen>.Collection.OneActive 的类中,Activated 事件就会毫无问题地触发。您可以尝试启用日志记录并查看 CM 在您尝试激活 VM 时做了什么:buksbaum.us/2010/08/08/how-to-do-logging-with-caliburn-micro
  • 嗨。这对我来说是非常有用的链接,谢谢。我会尝试必要的。我认为我在使用 AvalonDock 的某些方面遇到了麻烦......我做错了什么

标签: c# wpf caliburn.micro avalondock


【解决方案1】:

不要将文档对象添加到文档集合中,而是将它们添加到已经存在的 this.Items 集合中。

此外,每个文档对象都需要从 Screen 继承才能参与。

+should+ 足以解决问题,但有时可能需要告诉 Caliburn 通过 ConductWith 来“引导”您的视图模型...

document.ConductWith(this)

这是当前的导体视图模型。

【讨论】:

  • 非常感谢!...它与ConductWith 函数完美搭配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多