Dwayne Barsotta

Dwayne Barsotta

  • NA
  • 13
  • 5.4k

Caliburn Micro - Event Aggregator

Jan 28 2018 10:26 PM
I am trying to learn how I can pass data from a SecondWindowViewModel back to the ShellViewModel.  It looks like using messaging through event aggregator is what I want.  I have tried to get this to work on my primary project located on git hub:
 
I could not get it to work.  I have created a much simpler program.  T
The test program in here:
 
 https://github.com/Dev-Mech/CaliburnMicro.git
 
Can anyone offer a good tutorial on how to accomplish this, before I start messing around with the code again (add screwing it all up again)?  Please see the code in "ScheduleReview" for my first royal screw up.  My OCD is going crazy ATM. 
 
FYI - I deleted useless information from the original post. 
 
----------------  Edited  ---------------------------
 
This morning I rebuilt a simple application with two ViewModels. Both have a text box and a button.  In the starting viewmodel (ShellViewModel), when you click the "Click Me" button the second window opens.  Here the user types any text and clicks send.
 
I created a POCO to hold the data I want to send back (I know I could use just string, but my primary application requires sending more than one string, so I figured best to have it all stored in one object). 
 
I followed the directions from this page - right from the Calibrn.Micro authors:
 
https://caliburnmicro.com/documentation/event-aggregator 
 
The first time I ran the program VS errored with "no parameterless constructor for ShellViewModel" and stopped in the AppBootstrapper:
 
  1. DisplayRootViewFor();    
 So I created a parameterless constructor, the program runs normally until I hit send on the second window.  The program just breaks.  I think it is because I had to add the parameterless constructor in ShellViewModel and I am actually sending a "null" "_eventAggregator" to the second window.
 
I confirmed all my bindings are working. 
 
My code is below as well as on GitHub:
 
https://github.com/Dev-Mech/CaliburnMicro.git
 
AppBootstrapper:
  1. namespace CaliburnMicro  
  2. {  
  3.     class AppBootstrapper : BootstrapperBase  
  4.     {  
  5.         private readonly SimpleContainer _container = new SimpleContainer();  
  6.   
  7.         public AppBootstrapper()  
  8.         {  
  9.             Initialize();  
  10.         }  
  11.   
  12.         protected override void OnStartup(object sender, StartupEventArgs e)  
  13.         {  
  14.             DisplayRootViewFor();  
  15.         }  
  16.   
  17.         protected override void Configure()  
  18.         {  
  19.             _container.Singleton();  
  20.         }  
  21.     }  
  22. }  
ShellViewModel:
  1. namespace CaliburnMicro.ViewModels  
  2. {  
  3.     class ShellViewModel : Screen, IHandle  
  4.     {  
  5.         private string _messageBox;  
  6.         private readonly IEventAggregator _eventAggregator;  
  7.   
  8.   
  9.         public string MessageBox  
  10.         {  
  11.             get { return _messageBox; }  
  12.             set  
  13.             {  
  14.                 _messageBox = value;  
  15.                 NotifyOfPropertyChange(() => MessageBox);  
  16.             }  
  17.         }  
  18.   
  19.         public ShellViewModel(IEventAggregator eventAggregator)  
  20.         {  
  21.             _eventAggregator = eventAggregator;  
  22.             _eventAggregator.Subscribe(this);  
  23.         }  
  24.   
  25.         public ShellViewModel()  
  26.         {  
  27.   
  28.         }  
  29.   
  30.         public void OpenWindow()  
  31.         {  
  32.             WindowManager wm = new WindowManager();  
  33.             SecondWindowViewModel swm = new SecondWindowViewModel(_eventAggregator);  
  34.             wm.ShowWindow(swm);  
  35.         }  
  36.   
  37.         public void Handle(EventMessage message)  
  38.         {  
  39.             MessageBox = message.Text;  
  40.         }  
  41.     }  
  42. }  
SecondWindowViewModel: (BTW I attempted to change "PublishonUIThread" to whats in this code)
  1. namespace CaliburnMicro.ViewModels  
  2. {  
  3.     class SecondWindowViewModel: Screen  
  4.     {  
  5.   
  6.         private string _secondTextBox;  
  7.         private readonly IEventAggregator _eventAggregator;  
  8.         public EventMessage Tosend = new EventMessage();  
  9.   
  10.         public string SecondTextBox  
  11.         {  
  12.             get { return _secondTextBox; }  
  13.             set  
  14.             {  
  15.                 _secondTextBox = value;  
  16.                 NotifyOfPropertyChange(() => SecondTextBox);  
  17.             }  
  18.         }  
  19.           
  20.   
  21.   
  22.         public SecondWindowViewModel(IEventAggregator eventAggregator)  
  23.         {  
  24.             _eventAggregator = eventAggregator;  
  25.         }  
  26.   
  27.         public void SendBack()  
  28.         {  
  29.             Tosend.Text = SecondTextBox;  
  30.             _eventAggregator.PublishOnCurrentThread(Tosend);  
  31.             Thread.Sleep(1000); //I wanted the app to wait a second before closing  
  32.             TryClose();  
  33.         }  
  34.   
  35.     }  
  36. }  
 
 
 
 
 
 
 
 
 
 
 
 

Attachment: SceheduleReview.zip