0
votes

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?

1
Are you sure that you are not trying to activate the same Screen second time in a row? - Anton Kedrov
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
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
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<Screen>.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: buksbaum.us/2010/08/08/how-to-do-logging-with-caliburn-micro - Anton Kedrov
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

1 Answers

0
votes

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.