When I comment out the fm.OnLoaded
line below, it gives me an error that OnLoaded is null.
How can I make it optional for the caller of my class to consume the event or not as with .NET classes / events?
using System;
using System.Windows;
namespace TestEventLoaded8282
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
FileManager fm = new FileManager();
//fm.OnLoaded += new FileManager.LoadedHandler(fm_OnLoaded);
fm.Load();
}
void fm_OnLoaded(object obj, FileManagerArgs args)
{
Console.WriteLine("the file manager is loaded: " + args.Message);
}
}
public class FileManager
{
public string Name { get; set; }
public delegate void LoadedHandler(object obj, FileManagerArgs args);
public event LoadedHandler OnLoaded;
public FileManager()
{}
public void Load()
{
Name = "this is the test file manager";
OnLoaded(this, new FileManagerArgs("no errors"));
}
}
public class FileManagerArgs : EventArgs
{
public string Message { get; set; }
public FileManagerArgs(string message)
{
Message = message;
}
}
}