Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caliburn.Micro - doing something when a view becomes visible

I am currently getting into WPF and Caliburn.Micro ,for now without something like MEF or Autofac.

Right now i am trying to execute some code in a viewmodel right after its view becomes visible.

In a related tutorial this code displays a messagebox just before a view is shown:

protected override void OnActivate() 
{   
    MessageBox.Show("Page Two Activated"); //Don't do this in a real VM.   
    base.OnActivate();   
}  

Mr. Eisenberg then writes this:

Remember, if you have any activation logic that is dependent on the view being already loaded, you should override Screen.OnViewLoaded instead of/in combination with OnActivate.

This is what i have:

protected override void OnViewLoaded(object view)
{
    base.OnViewLoaded(view);
    MessageBox.Show("OnPageTwoViewLoaded");
}

I also tried it via a Grid EventTrigger and a cal:ActionMessage. But in all three cases the MessageBox appears before the view is visible.

Surely i am missing something, what am i doing wrong?

like image 559
user1018465 Avatar asked Oct 28 '11 16:10

user1018465


1 Answers

Maybe not the most elegant solution, but I guess you can do this from the code-behind, since - strictly speaking - this is a very view/gui specific thing you're trying to do here. For instance in OnInitialized or OnRender. If you give your view a reference to the EventAggregator, you could raise an event and make the view model - or whatever class you want, subscribe to this event and do it's thing. Or in the case of showing a MessageBox, you really wouldn't have that any place else than in the View anyway.

like image 154
Kjetil Klaussen Avatar answered Oct 12 '22 06:10

Kjetil Klaussen