簡體   English   中英

將通用 PubSubEvents 用於 Prism Eventaggregator WPF

[英]Using generic PubSubEvents for Prism Eventaggregator WPF

我正在嘗試從 Prism 的EventAggregator包裝對事件的訪問,以實現可重用性

這是一個示例代碼

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' 必須是具有公共無參數構造函數的非抽象類型才能將其用作參數....

假設這在 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的鍋爐點,每次我調用Subscription時手動調用GetEvent

例子:

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

我一直缺少的是泛型類型T1new()約束。

顯然,Prism IEventAggregator 的GetEvent<T>要求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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM