Caliburn.Micro EventAggregator [英] Caliburn.Micro EventAggregator

查看:208
本文介绍了Caliburn.Micro EventAggregator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个非常愚蠢的问题表示歉意,但是我才刚刚开始使用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...

我不确定问题出在视图模型还是引导程序.这是视图模型:

I'm not sure whether the problem is with the view model or the bootstrapper. Here is the 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);
    }
}

引导程序:

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...

我正在使用SimpleInjector作为IOC容器

I am using SimpleInjector as the IOC Container

似乎很简单,我不知道自己在做什么. RTFM.

It seems like a very simple case of I didn't know what I was doing. RTFM.

实现IHandle确实有效.不过,似乎在第一次处理该类型时就被调用过两次.我将对原因进行调查.

Implementing IHandle does work. 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.

我相信只要您使用与发布时兼容的类型实现IHandle<T>接口,它就可以工作.例如:

I believe it should work provided you implement an IHandle<T> interface using a type compatible with the even you're publishing. E.g:

class MainWindowViewModel : Screen, IHandle<string>
{ 
    //... Your Code

    public void Handle(string myEventstring)
    {
        // Do Something.
    }
}

如果有帮助的话,当我使用EventAggregator时,我倾向于创建一个静态EventAggregator实例(来自小型助手类),该实例可以在需要它的任何ViewModels中使用-在某些情况下可能会有所帮助您实际上是偶然地多次初始化了EventAggregator的地方(可能是导致双重事件的原因).

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).

有时我还会创建一些小的帮助程序类来包装事件信息.例如:

I also sometimes create small helper classes to wrap up event information. E.g:

public sealed class DownloadFinishedEvent
{
    public readonly string EventText = "Download Completed";

    // Additional Download Info Here.

    public override string ToString()
    {
        return this.EventText;
    }
}

这篇关于Caliburn.Micro EventAggregator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆