I'm looking to create a Button class in my custom XNA GUI that accepts methods as an argument, similar to how, in Python's tkinter
you can just set the function to be called with Button.config(command = a_method)
.
I've read about using delegates as parameters here, here and here, but I don't seem to be any closer to getting it working. I don't fully understand how delegates work, but I've tried several different things unsuccessfully, like using Func<int>? command = null
in order to test later to see if command
is null
then I'd call the preset default, but then I get a Func cannot be nullable type
or something similar.
Ideally the code'd be something like:
class Button
{
//Don't know what to put instead of Func
Func command;
// accepts an argument that will be stored for an OnClick event
public Button(Action command = DefaultMethod)
{
if (command != DefaultMethod)
{
this.command = command;
}
}
}
But it seems like everything I've tried is not working out.