Differences between revisions 1 and 2
Revision 1 as of 2020-04-17 10:59:59
Size: 546
Editor: Burathar
Comment:
Revision 2 as of 2020-04-17 11:02:51
Size: 1152
Editor: Burathar
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
=== Standard import === === Custom Events & Handling ===
CustomEventArgs.cs
Line 28: Line 29:

Instance.cs
{{{#!highlight c#
public delegate void NotifyCustomEvent(object sender, CustomEventArgs e);
public event NotifyCustomEvent CustomEvent;

private void RaiseCustomEvent(obj sender, obj item)
{
    CustomEvent?.Invoke(sender, new CustomEventArgs(item));
}
}}}


InstanceOwner.cs
{{{#!highlight c#
public InsanceOwner()
{
  Instance instance = new instance();
  instance.CustomEvent += new Instance.NotifyCustomEvent(CustomEventHandler);
}

public void CustomEventHandler(obj sender, CustomEventArgs e)
{
    //Handle that custom shit
}
}}}

Description

The snippets on this wikipage are written for the .NET Framework v4.0 and higher, but most probably work in older versions. Most will, however not work for .NET Core and .NET Standard.

Snippets

Custom Events & Handling

CustomEventArgs.cs

   1 public class CustomEventArgs : EventArgs
   2 {
   3     private readonly object item;
   4 
   5     public object Item
   6     {
   7         get { return item; }
   8     }
   9 
  10     public CustomEventArgs(object item)
  11     {
  12         this.item = item;
  13     }
  14 }

Instance.cs

   1 public delegate void NotifyCustomEvent(object sender, CustomEventArgs e);
   2 public event NotifyCustomEvent CustomEvent;
   3 
   4 private void RaiseCustomEvent(obj sender,  obj item)
   5 {
   6     CustomEvent?.Invoke(sender, new CustomEventArgs(item));
   7 }

InstanceOwner.cs

   1 public InsanceOwner()
   2 {
   3   Instance instance = new instance();
   4   instance.CustomEvent += new Instance.NotifyCustomEvent(CustomEventHandler);
   5 }
   6 
   7 public void CustomEventHandler(obj sender, CustomEventArgs e)
   8 {
   9     //Handle that custom shit
  10 }

Howto/CSharp (last edited 2020-04-20 11:02:18 by Burathar)