在 Singleton 类上创建事件

发布于 2024-10-07 14:33:28 字数 675 浏览 0 评论 0原文

我有一个 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;
        }
    }
}

reference

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2024-10-14 14:33:28

有什么问题吗?

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

What's the problem?

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
}

Call OnLocationChanged whenever you want to fire the event.

菊凝晚露 2024-10-14 14:33:28

您应该能够像在任何班级的活动中一样执行此操作。

public event Action<object, EventArgs> LocationChanged;

然后,您可以拥有受保护的虚拟方法,例如:

protected virtual void OnLocationChanged(EventArgs args)
{
   if(LocationChanged != null)
   {
     LocationChanged(this, args);
   }
}

您也可以在需要时触发 OnLocationChanged 方法,并且您附加的事件将执行其操作。

You should just be able to do this as you would an event on any class.

public event Action<object, EventArgs> LocationChanged;

you can then have a protected virtual method such as:

protected virtual void OnLocationChanged(EventArgs args)
{
   if(LocationChanged != null)
   {
     LocationChanged(this, args);
   }
}

You can fire off your OnLocationChanged method where ever you need too and the event's you've attached will do their thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文