0
votes

I am learning WPF with Caliburn Micro. I have read the documentation many times and I am even following tutorial on YouTube by Timcorey. Somewhere along the line I must have not specified/initialized something correctly.

Normally I would specify the object as X obj = new X(); but in this case the eventaggregator does not like it. I did manage to get the code to run by changing the events.subscribe line to :

if (_events != null) _events.Subscribe(this)

but during runtime the code never reaches this line even when a breakpoint is set. With all the eventaggregator code removed, I can run and trigger my events. I just can't seem to publish and subscribe to it.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PropertyChanged;
using Caliburn.Micro;

using ERP101.EventModels;
using ERP101.ViewModels;

namespace ERP101.ViewModels
{
    [AddINotifyPropertyChangedInterface]
    public class ShellViewModel : Conductor<object>,IHandle<LoginEvent>
    {
        private IEventAggregator _events;
        private StartPageViewModel _startPVM;
        private SimpleContainer _container;

        public ShellViewModel(IEventAggregator events,StartPageViewModel startPVM,SimpleContainer container)
        {
            _events = events;
            _events.Subscribe(this); //null reference error here
            _startPVM = startPVM;
            _container = container;

             ActivateItem(_container.GetInstance<LoginViewModel>());

        }

        public void Handle(LoginEvent message)
        {
            ActivateItem(_startPVM);
        }
    }
}```

1
IEventAggregator events is apparently null. We can't say why, that code isn't part of the question.user47589

1 Answers

0
votes

Thanks Amy, So I went again back to the tutorials and I found my issue in the container code.


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

            GetType().Assembly.GetTypes()
                .Where(type => type.IsClass)
                .Where(type => type.Name.EndsWith("ViewModel"))
                .ToList()
                .ForEach(viewModelType => _container.RegisterPerRequest(viewModelType, viewModelType.ToString(), viewModelType));
        }

.Singleton<EventAggregator, EventAggregator>(); - this line is incorrect, the corrected line is in code above. The first had to be an interface type.