在Singleton类上创建事件 [英] Create Event on Singleton Class
本文介绍了在Singleton类上创建事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Windows Mobile 6.5(.net cf 3.5),它使用遵循以下模式的单例类:
I have a Windows Mobile 6.5 (.net cf 3.5) that uses a singleton class which follows this pattern:
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
我的课程用于从中间硬盘中收集GPS数据。我要在单例类上创建一个可以订阅的事件?例如。 MyClass.Instance.LocationChanged + = ...;
My class used to collect GPS data from the Intermediate drive. What I want is to create an event on the singleton class that I can subscribe to? E.g. MyClass.Instance.LocationChanged += ...;
任何帮助将不胜感激。
Mark
推荐答案
出了什么问题?
public sealed class Singleton
{
... your code ...
public delegate LocationChangedEventHandler(object sender, LocationChangedEventArgs ea);
public event LocationChangedEventHandler LocationChanged;
private void OnLocationChanged(/* args */)
{
if (LocationChanged != null)
LocationChanged(this, new LocationChangedEventArgs(/* args */);
}
}
public class LocationChangedEventArgs : EventArgs
{
// TODO: implement
}
每当您要触发事件时,呼叫 OnLocationChanged
。
Call OnLocationChanged
whenever you want to fire the event.
这篇关于在Singleton类上创建事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文