我正在与Caliburn Micro学习WPF。我已经阅读了很多次文档,甚至正在关注Timcorey在YouTube上的教程。我一定没有正确指定/初始化一些东西。
通常我将对象指定为X obj = new X();但是在这种情况下,eventaggregator不喜欢它。我确实通过将events.subscribe行更改为:
来使代码得以运行。if (_events != null) _events.Subscribe(this)
但是在运行时,即使设置了断点,代码也永远不会到达此行。删除所有eventaggregator代码后,我可以运行并触发事件。我似乎无法发布和订阅它。
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);
}
}
}```
答案 0 :(得分:0)
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.