[英]WeakEventManager and static event
I am trying to get used to WeakEventManager
and I stumble with following: 我试图习惯
WeakEventManager
,我偶然发现:
The only difference between A
and B
is static
, please ignore copy/paste error with nameof
;) A
和B
之间的唯一区别是static
,请忽略带有nameof
复制/粘贴错误;)
I found this answer regarding generics and static types, but I wonder what WeakEventManager
is doing with A
then? 我找到了关于泛型和静态类型的答案 ,但我想知道
WeakEventManager
用A
做什么呢? Somehow it can work with null
as source of static event. 不知何故,它可以使用
null
作为静态事件的源。
I am seeking for a simple answer why static
event is ok, but static class
as TEventSource
suddenly is not. 我正在寻找一个简单的答案,为什么
static
事件是好的,但static class
作为TEventSource
突然不是。
Code: 码:
public class A
{
public static event EventHandler Event;
}
public static class B
{
public static event EventHandler Event;
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WeakEventManager<A, EventArgs>.AddHandler(null, nameof(A.Event), (s, e) => { });
WeakEventManager<B, EventArgs>.AddHandler(null, nameof(B.Event), (s, e) => { });
}
}
Error: 错误:
Error CS0718
错误CS0718
'B': static types cannot be used as type arguments'B':静态类型不能用作类型参数
WeakEventManager
can deal with static events, when source
is null : 当
source
为null时, WeakEventManager
可以处理静态事件:
object sourceKey = (source != null) ? source : StaticSource;
where StaticSource
is a special "event source" for static events. 其中
StaticSource
是静态事件的特殊“事件源”。
This is implementation details of WeakEventManager
, and that's why it's OK. 这是
WeakEventManager
实现细节,这就是为什么它没问题。
About static types as generic parameter - this is language limitation. 关于静态类型作为通用参数 - 这是语言限制。 It isn't related specifically to
WeakEventManager
. 它与
WeakEventManager
无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.