[英]Caliburn.Micro EventAggregator
Apologise if this a really stupid question but I'm just getting started with caliburn.micro and I'm struggling with getting the eventAggregator, nothing seems to be subscribing... 请道歉,如果这是一个非常愚蠢的问题,但我刚开始使用caliburn.micro并且我正在努力获得eventAggregator,似乎没有任何订阅...
I'm not sure whether the problem is with the view model or the bootstrapper. 我不确定问题是视图模型还是引导程序。 Here is the viewmodel:
这是viewmodel:
class MainWindowViewModel : Screen
{
private readonly IEventAggregator _eventAggregator;
public MainWindowViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(this);
}
public void SayHello()
{
_eventAggregator.Publish("Hello World!");
}
public void Handle(string message)
{
MessageBox.Show(message);
}
}
Bootstrapper: 引导程序:
class AppBootstrapper : Bootstrapper<MainWindowViewModel>
{
public static readonly Container ContainerInstance = new Container();
protected override void Configure()
{
ContainerInstance.Register<IWindowManager, WindowManager>();
ContainerInstance.RegisterSingle<IEventAggregator,EventAggregator>();
ContainerInstance.Register<MainWindowViewModel, MainWindowViewModel>();
ContainerInstance.Verify();
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return ContainerInstance.GetAllInstances(service);
}
protected override object GetInstance(System.Type service, string key)
{
return ContainerInstance.GetInstance(service);
}
protected override void BuildUp(object instance)
{
ContainerInstance.InjectProperties(instance);
}
}
Any ideas what I'm missing, I feel I must not be linking somewhere... 任何我缺少的想法,我觉得我一定不能在某个地方联系......
I am using SimpleInjector as the IOC Container 我使用SimpleInjector作为IOC容器
EDIT: 编辑:
It seems like a very simple case of I didn't know what I was doing. 这似乎是一个非常简单的例子,我不知道自己在做什么。 RTFM.
RTFM。
Implementing IHandle does work. 实现IHandle确实有效。 It seems to get called twice the first time the type is handled though.
它似乎在第一次处理类型时被调用了两次。 I'll do some investigating as to why.
我会做一些调查,为什么。
It sounds like you've already arrived at a solution of sorts. 听起来你已经达到了各种各样的解决方案。
I believe it should work provided you implement an IHandle<T>
interface using a type compatible with the even you're publishing. 我相信如果您使用与您正在发布的兼容类型实现
IHandle<T>
接口,它应该可以工作。 Eg: 例如:
class MainWindowViewModel : Screen, IHandle<string>
{
//... Your Code
public void Handle(string myEventstring)
{
// Do Something.
}
}
If at all helpful, when I use the EventAggregator
, I tend to create a static EventAggregator
instance (from a small helper class) which I use in any ViewModels
that require it - it may help in cases where you've actually initialised the EventAggregator
multiple times by accident (might be the cause of your double event). 如果有用,当我使用
EventAggregator
,我倾向于创建一个静态EventAggregator
实例(来自一个小帮助器类),我在任何需要它的ViewModels
中使用它 - 它可能有助于你实际初始化EventAggregator
多个偶然的次数(可能是你的双重事件的原因)。
I also sometimes create small helper classes to wrap up event information. 我有时也会创建小帮助程序类来包装事件信息。 Eg:
例如:
public sealed class DownloadFinishedEvent
{
public readonly string EventText = "Download Completed";
// Additional Download Info Here.
public override string ToString()
{
return this.EventText;
}
}
The caliburn micro doc example shows, that the subscriber has to implement the IHandle interface. caliburn micro doc示例显示,订户必须实现IHandle接口。 I think that's the problem.
我认为这就是问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.