I know that this topic is shown everywhere on SO, but I couldn’t find anything specific to this. I have a class that is used to store all my created windows for my application. What I am trying to do is create a new window, if one does not exist, and attach some events to it. Problem is that the events are throwing the error.
The below snippet of code is in the class for when an event happens a new window is supposed to be created. Below that are two event handler definitions. These are all in the same class.
Window window = FindWindow(windowId);
if (window == null)
{
window = new Window();
window.Closing += new System.ComponentModel.CancelEventHandler(window_Closing);
window.Closed += new EventHandler(window_Closed);
_winDict.Add(windowId, window);
}
window.Owner = Application.Current.MainWindow;
window.Title = title;
window.Content = guc;
window.SizeToContent = SizeToContent.WidthAndHeight;
window.ResizeMode = ResizeMode.NoResize;
window.ShowInTaskbar = false;
Code for the two event handlers
void window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
throw new NotImplementedException();
}
void window_Closed(object sender, EventArgs e)
{
throw new NotImplementedException();
}
Hopefully this is enough information to help me debug the issue.
Thanks in advance.
If this code snippet appears within a static method then there is no ‘this’ pointer, and you cannot access the window_Closing and window_Closed methods.
Try making window_Closed and window_Closing static, to see if it compiles – but be warned that they will also not have a ‘this’ pointer.
What is probably the ‘real’ fix is of course is to make the method non-static, and in the current static method create an instance of the class then call that non-static method.
(Note: not compiled and tried this snippet)