在 Singleton 类上创建事件
我有一个 Windows Mobile 6.5 (.net cf 3.5),它使用遵循以下模式的单例类:
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 += ...;
任何帮助将不胜感激。
标记
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;
}
}
}
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 += ...;
Any help would be greatly appreciated.
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有什么问题吗?
每当您想要触发该事件时,请调用
OnLocationChanged
。What's the problem?
Call
OnLocationChanged
whenever you want to fire the event.您应该能够像在任何班级的活动中一样执行此操作。
然后,您可以拥有受保护的虚拟方法,例如:
您也可以在需要时触发 OnLocationChanged 方法,并且您附加的事件将执行其操作。
You should just be able to do this as you would an event on any class.
you can then have a protected virtual method such as:
You can fire off your OnLocationChanged method where ever you need too and the event's you've attached will do their thing.