c# - 为 Prism Eventaggregator WPF 使用通用的 PubSubEvents

标签 c# wpf prism eventaggregator

我正在尝试包装对 Events 的访问来自 EventAggregator Prism 的可重用性

这是一个示例代码

protected void Subscription<T1,T2>(Action<T2> OnSubcribe) where T1:CachedEvent<T2>
{
    T1 @event  = _eventAggregator.GetEvent<T1>();
    event.Subcribe(OnSubcribe,true);
    //Some Codes other codes
}

我在 T1 上设置了约束条件, CachedEvent ,它是 PubSubEvent<TPayload> 的非抽象派生类

但是还是报错

'T1' must be a non-abstract type with a public parameterless constructor in order to use it as parameter....

假设这在 C# 中不是真的有效,还有其他替代方案吗?

更新:

这是我到目前为止所做的 我做了@event一个参数

protected void Subscription<T1,T2>(T1 @event, Action<T2> OnSubcribe) where T1:CachedEvent<T2>
{
    @event.Subcribe(OnSubcribe,true);
    //Some Codes other codes
}

我的应用程序可以运行,但我仍然无法摆脱创建 @event 的锅炉点, 调用 GetEvent手动,每次我调用Subscription

例子:

Subcription(eventAggregator.GetEvent<SomeEventBasedOnCachedEvent>(),AMethodDoneOnSubcribe);

最佳答案

我一直想念的是 new() 通用类型的约束 T1 .

显然 GetEvent<T> Prism IEventAggregator要求 T必须有一个无参数的构造函数。

protected void Subscription<T1,T2>(Action<T2> OnSubcribe) where T1:CachedEvent<T2>,new()
{
    T1 @event  = _eventAggregator.GetEvent<T1>();
    event.Subcribe(OnSubcribe,true);
    //Some Codes other codes
}

关于c# - 为 Prism Eventaggregator WPF 使用通用的 PubSubEvents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49895195/

相关文章:

c# - 数组何时出错?

c# - 关于构造函数

c# - PromptDialog 用于未确定数量的问题

c# - 我如何测试 Relaycommand?

wpf - 如何使用 MEF 和 MVVM 将 PRISM 模块添加到工具栏

c# - UWP 导航导致访问冲突

c# - 将数量封装在结构中以实现类型安全会对性能产生影响吗?

wpf - 如何在 WPF MVVM 应用程序中处理可绑定(bind)应用程序范围的变量?

c# - WPF中ViewModels之间如何通信以及如何控制Views生命周期

c# - PnP WPF/Silverlight 复合应用程序指南是否适合基于角色的 UI 组合?