Pages

Thursday, 18 April 2013

WPF Error: Why double clicking my contorls does not generate events in WPF?

WPF Error: Why double clicking my contorls does not generate events in WPF?
 
While working with WPF application, I created a button and when I double clicked it to generate the event of the button, I found following error.
 
To generate an event handler the class 'MainWindow', specified by the x:Class or x:Subclass attribute, must be the first class in the file. Move the class code so that it is the first class in the file and try again.
 
How did I resolve this error:
 
I looked into my xaml.cs file, the structure of that file was like this:
 
namespace WPFApplication1
{
   class MyNewClass
   {
   }
   public partial class MainWindow : Window
   {
     public MainWindow()
     {
       InitializeComponent();
     }
   }
}
 
I had created my new class "MyNewClass" above the declaration of the default MainWindow class. Due to this, double clicking the button was not able to generate the event automatically. When I cut this class and pasted below the MainWindow class, everything went fine. Like below:
 
namespace WPFApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    class MyNewClass
    {
    }
}
 
Well, to be honest, I don't know the exact reason for this, but I thought sharing this situation might help others and if any of you know the exact reason for this, please share it.

No comments:

Post a Comment