简体   繁体   English

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

相关问题 具有多个实例的Caliburn.Micro EventAggregator - Caliburn.Micro EventAggregator with multiple instances ShellViewModel处理eventAggregator Caliburn.Micro上的多个消息 - ShellViewModel Handling multiple messages on the eventAggregator, Caliburn.Micro caliburn.micro eventAggregator:处理屏幕集合中的消息 - caliburn.micro eventAggregator: Handling message in Screen Collection Caliburn.Micro中的UserControl - UserControl in Caliburn.Micro 带有Caliburn.Micro的ModalContentPresenter - ModalContentPresenter with Caliburn.Micro Caliburn.micro与团结 - Caliburn.micro with unity 尝试使用 EventAggregator 时,Caliburn.Micro 的 DisplayRootViewFor 方法中的 NullReferenceException - NullReferenceException in Caliburn.Micro's DisplayRootViewFor method when trying to use EventAggregator 如何使用 Caliburn.micro EventAggregator MVVM 在 ViewModel 之间传递值 - How to pass values between ViewModels using Caliburn.micro EventAggregator MVVM 来自 Caliburn.Micro 的 EventAggregator.PublishOnUIThread 抛出:System.InvalidOperationException:“未使用调度程序初始化。” - EventAggregator.PublishOnUIThread from Caliburn.Micro throws : System.InvalidOperationException: 'Not initialized with dispatcher.' Caliburn.Micro EventAggregator.PublishOnUIThreadAsync “tasks 参数包含一个空值。参数名称:tasks” - Caliburn.Micro EventAggregator.PublishOnUIThreadAsync "The tasks argument included a null value. Parameter name: tasks"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM