C# Windows Phone正在添加来自不同类的eventhandler
使用这段代码,我将向RootFrame.obclused添加一个eventhandlerC# Windows Phone正在添加来自不同类的eventhandler,c#,windows-phone-7,event-handling,C#,Windows Phone 7,Event Handling,使用这段代码,我将向RootFrame.obclused添加一个eventhandler (Application.Current as App).RootFrame.Obscured += onObScured; 由于可以从应用程序中的每个类访问根框架,如果我从不同的类添加不同的EventHandler会发生什么? 例如: 触发事件时,如果同时创建了A和B的实例,是否会同时触发onObScuredA()和onObScuredB()? 在App.xaml.cs类上添加EventHandler及
(Application.Current as App).RootFrame.Obscured += onObScured;
由于可以从应用程序中的每个类访问根框架,如果我从不同的类添加不同的EventHandler会发生什么?
例如:
触发事件时,如果同时创建了A和B的实例,是否会同时触发onObScuredA()和onObScuredB()?
在App.xaml.cs类上添加EventHandler及其各自的方法是否正确,以便确定添加了哪些EventHandler
谢谢。您可以在事件中添加任意数量的事件处理程序,它们都将被调用。这就是事件的本质,当它们触发时,所有事件处理程序都会处理此事件 因此,答案是“它们都会被触发”。现在,这可能是您想要的,也可能不是您想要的,但是添加新的事件处理程序不会替换以前的事件处理程序
阅读MSDN:。的更多信息。因此,如果我在事件中添加类似的内容。myvar=“hello”,如果不存在该类的实例,它将引发异常,并为当前类型创建的每个对象将myvar设置为“hello”?如果尚未创建实例,则永远不会将onObScrutedA方法添加到eventhandler?
class A{
(Application.Current as App).RootFrame.Obscured += onObScuredA;
private void onObScuredA(object sender, ObscuredEventArgs e) {
//Some code here
}
}
class B{
(Application.Current as App).RootFrame.Obscured += onObScuredB;
private void onObScuredB(object sender, ObscuredEventArgs e) {
//Some other code here
}
}