0

I'm using Caliburn Micro and AvalonDock in my project. I try to handle events when screen was activated. My main view model is a 'Conductor.Collection.OneActive' and each tab "document" is a 'Screen'. I have a function in my main view model like this:

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

But the function "Document_Activated" is not invoked. what am I doing wrong?

Anton Kedrov
  • 1,767
  • 2
  • 13
  • 20
  • Are you sure that you are not trying to activate the same Screen second time in a row? – Anton Kedrov Sep 01 '16 at 09:19
  • And just note. There is no need to add document into `Items` collection manually, it will be added when you call `ActivateItem(document)`. And it seems like `_documents` and `ActiveDocument` are just duplicating built-in conductor properties: `Items` and `ActiveItem`. – Anton Kedrov Sep 01 '16 at 09:21
  • Thanks for your reply. Yes, `_documents` and `Items` looks the same and they are )... it's a result of any my experiments. I was looking for decision. I don't understand why it not working. When the `Activated` event should invoke?... And, yes, I'm not trying to activate the same Screen second time. – R. Bolshakov Sep 02 '16 at 09:49
  • I wasn't able to reproduce this issue with Caliburn.Micro 3.0.1.0. I have made simple 'DocumentViewModel' class, which is derived from the 'Screen' class and as soon as I call `ActivateItem(document)`, in the class derived from `Conductor.Collection.OneActive`, `Activated` event fires without any trouble. You might try to enable logging and see what CM does when you try to activate your VM: http://buksbaum.us/2010/08/08/how-to-do-logging-with-caliburn-micro/ – Anton Kedrov Sep 05 '16 at 10:03
  • Hi. This is very usefull link for me, thanks. I'll try neccesarily. I think my trouble in some aspect of AvalonDock using... something I'm doing wrong – R. Bolshakov Sep 06 '16 at 13:07
  • I've created a simple app to check activations. It works good: `Activated` event fires when I run `ActivateItem(child)`. Also I've created a simple loggin with Caliburn ILog interface in both my applications. In simple app I see two messages `INFO:Activating wpf_mvvm_test.ViewModels.MainViewModel. INFO: Activating wpf_mvvm_test.ViewModels.ChildViewModel.` but in my main app I don't see that messages. Error and Warn messages are missing. – R. Bolshakov Sep 06 '16 at 13:58

1 Answers1

0

Instead of adding your document objects into a documents collection, add them to the already existing this.Items collection.

Also, each document object will need to inherit from Screen for it to participate.

That +should+ be enough to do the trick, but sometimes it can be necessary to tell Caliburn to "conduct" your viewmodels via ConductWith...

document.ConductWith(this)

there this is the current conductor viewmodel.

DarinH
  • 4,868
  • 2
  • 22
  • 32