Using a separate class which is being made reusable, need to pass it a function which would be called on the click event from the context menu…
Problem : Functions aren’t type EventHandlers…. also Different EventHandlers require different paramters inside… e.g. the OnClose for the exit button….
Edit:
In the Class X
public void AddMenuItem(String name, EventHandler target )
{
MenuItem newItem = new MenuItem();
newItem.Index = _menuItemIndex++;
newItem.Text = name;
newItem.Click += target;
_contextMenu.MenuItems.Add(newItem);
}
In the Wpf:
addToTray.AddMenuItem("&Exit", Exit);
I would love it to link to the following method but at this point any method would do fine.
private void ShouldIExit(object sender, System.ComponentModel.CancelEventArgs e)
{
// checks if the Job is running and if so prompts to continue
if (_screenCaptureSession.Running())
{
MessageBoxResult result = System.Windows.MessageBox.Show("Capturing in Progress. Are You Sure You Want To Quit?", "Capturing", MessageBoxButton.YesNo);
if (result == MessageBoxResult.No)
{
e.Cancel = true;
return;
}
}
_screenCaptureSession.Stop();
_screenCaptureSession.Dispose();
}
I’m guessing that the problem is that your
ShouldIExitmethod does not match theEventHandlerdelegate. Try changing it to take a regularEventArgsparameter, and see if that works. It’s best to avoid reusing the same event handler for different event types. You should encapsulate common code into separate methods, and then have different handlers call that code.