Question Caliburn.Micro Event Aggregator

DwayneB76

New member
Joined
Jan 29, 2018
Messages
1
Programming Experience
Beginner
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 with no luck, so I created a very simple program. the "ShellViewModel" has a text box and a button, when you click the button another window opens, the second window has a button and a text box. When the user types something in the text box and clicks the button, the program is SUPPOSED to send the text back to the primary window where it gets displayed. My program is posted public here:

https://github.com/Dev-Mech/CaliburnMicro.git

right now when I click the button I am getting a "null reference" error. I believe the "_eventAggregator" object is not being created in this line.


C#:
_eventAggregator.PublishOnCurrentThread(Tosend);

I have confirmed all my data bindings work. I used a custom POCO as the message since in my primary program I will be sending an object back, not just simple text. I have followed several tutorials, all say different things and non were able to work right. The simple program I made is based on this tutorial - right from Caliburn Micro's site:

https://caliburnmicro.com/documentation/event-aggregator

If anyone can help me with this, I would be very appreciative. I can't seem to find a lot of documentation on this!

"AppBootstrapper"
C#:
namespace CaliburnMicro{
    class AppBootstrapper : BootstrapperBase
    {
        private readonly SimpleContainer _container = new SimpleContainer();


        public AppBootstrapper()
        {
            Initialize();
        }


        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor<ShellViewModel>();
        }


        protected override void Configure()
        {
            _container.Singleton<IEventAggregator, EventAggregator>();
        }
    }
}

"ShellViewModel"

C#:
namespace CaliburnMicro.ViewModels{
    class ShellViewModel : Screen, IHandle<EventMessage>
    {
        private string _messageBox;
        private readonly IEventAggregator _eventAggregator;




        public string MessageBox
        {
            get { return _messageBox; }
            set
            {
                _messageBox = value;
                NotifyOfPropertyChange(() => MessageBox);
            }
        }


        public ShellViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _eventAggregator.Subscribe(this);
        }


        public ShellViewModel()
        {


        }


        public void OpenWindow()
        {
            WindowManager wm = new WindowManager();
            SecondWindowViewModel swm = new SecondWindowViewModel(_eventAggregator);
            wm.ShowWindow(swm);
        }


        public void Handle(EventMessage message)
        {
            MessageBox = message.Text;
        }
    }
}

"SecondWindowViewModel"
C#:
namespace CaliburnMicro.ViewModels{
    class SecondWindowViewModel: Screen
    {


        private string _secondTextBox;
        private readonly IEventAggregator _eventAggregator;
        public string SecondTextBox
        {
            get { return _secondTextBox; }
            set
            {
                _secondTextBox = value;
                NotifyOfPropertyChange(() => SecondTextBox);
            }
        }
        




        public SecondWindowViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }


        public void SendBack()
        {
            EventMessage Tosend = new EventMessage();
            Tosend.Text = SecondTextBox;
            _eventAggregator.PublishOnCurrentThread(Tosend);
            Thread.Sleep(1000); //I wanted the app to wait a second before closing
            TryClose();
        }


    }
}
 
Back
Top Bottom