订阅独立 WPF 加载项中的事件 [英] Subscring to events in an isolated WPF add-in

查看:55
本文介绍了订阅独立 WPF 加载项中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个独立的 WPF 加载项.插件获取对公开事件的类型的引用.由于插件被隔离在特定的应用程序域中,主机对插件的 DLL 或存储在那里的类型一无所知.

I have an isolated WPF add-in. The addin gets a reference to the type that exposes the event. Since addin is isolated in a specific appdomain, the host knows nothing about the addin's DLL, or the types stored there.

合同:

    [AddInContract]
    public interface IAddInContract: IContract
    {
        void Initialize(IAddInHandler handler);
    }

handler 是一种暴露事件的类型,它派生自 MarshalByRef,以便将代理传递给插件:

handler is a type that exposes the event, and derives from MarshalByRef, so that the proxy is passed to the addin:

    public class AddInHandler : MarshalByRefObject, IAddInHandler 
    {
       public event SelectionChangedEventHandler SelectionChanged;
    }

在插件中,我们执行以下操作:

and in the addin we do something like:

        public override void Initialize(IAddInHandler handler)
        {
            handler.SelectionChanged += handler_SelectionChanged;
        }

然而,这失败了,因为它试图(以某种方式)将插件的引用传递给宿主应用程序,并且由于对于宿主,插件的 dll 是未知的,它无法加载类型并引发异常.我如何仍然保持隔离,并能够执行上述操作?

However this fails, as it tries (somehow) to pass a reference of the addin to the host application, and since to the host, addin's dll is unknown, it fails to load the type and throws exception. How can I still maintain the isolation, and be able to do something like described above?

推荐答案

我遇到了类似的问题,通过以下方式解决了:

i had a similar problem and solved it in the following way:

对于插件合同,我没有使用使用委托的 C# 事件模式.我使用了Java-Like"事件注册模式,这意味着我用一个方法创建了一个接口(接口需要实现 IContract),然后我在合同中用 AddEventHandler 和 RemoveEventHandler 创建了两个方法.两者都需要先前创建的接口的实例.在适配器中,您在委托和接口实现之间创建了一个适配器.

For the Add-In Contract, I didn't used the C# Eventing Pattern using Delegates. I used a "Java-Like" Event Registration Pattern, that means i created an interface with one Method (Interface needs to implement IContract), then i created in the Contract two methods with AddEventHandler and RemoveEventHandler. Both requires an instance of the previously created interface. In the Adapters, you create an adapter between your delegate and that interface implementation.

这篇关于订阅独立 WPF 加载项中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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