简体   繁体   English

如何在Unity容器中自动注册事件

[英]How to auto register event in Unity Container

I have a class with some event: 我上课有一些活动:

public class Foo

    {
        public event FooEventHandler FooEvent;

        public static void FooEvent(object sender, FooEventArgs fooEventEventArgs)
        {
            //...
        }
    }

This class used with Unity 1.2(because I am using it in .Net Framework 2.0). 此类与Unity 1.2一起使用(因为我在.Net Framework 2.0中使用了它)。 Also I have FooRepository class, which implement business logic. 我还有FooRepository类,它实现业务逻辑。 In this class I want to add private method OnFooEvent() . 在此类中,我想添加私有方法OnFooEvent() How auto register this event in Unity container? 如何在Unity容器中自动注册此事件?

IUnityContainer _cont=new UnityContainer();
_cont.RegisterType<IRepository,FooRepository>();
var foo=_cont.Resolve<FooRepository>();

Have you looked at the EventBroker quickstart included with the Unity source code? 您是否看过Unity源代码附带的EventBroker快速入门? Wiring events aren't something included by default, but the quickstart includes a sample extension that will auto-wire event senders and receivers. 默认情况下不包括接线事件,但快速入门包含一个示例扩展,它将自动连接事件发送方和接收方。

Another option would be to look at the Prism composite application guidance . 另一个选择是查看Prism复合应用程序指南 There's something in there called EventAggregator which can be used independently to also give you some of these capabilities. 其中有一个称为EventAggregator的东西,可以独立使用它来为您提供其中一些功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM