简体   繁体   中英

Visual Studio 2013 and C# - Unable to add event handler

I am working on WPF project, and after I made everything in Xaml file I wanted to start doing on my CS file. Now, I was trying to add event using that little bolt in properties, but everytime i click on any event (for example I select button in XAML and try to add -> MyButton_Click) i get a pop-up message "UNABLE TO ADD EVENT HANDLER".

I hope you are able to help me.

yes, @will is right, I also suffered from the same problem,when i was new in WPF. Please check your x:Class in your XAML file, it should include the namespace used in code behind. eg, x:Class="MainWindow" should be some thing like x:class="namespace.MainWindow" if it doesn't work,try to restart the VS or try to recreate the project with correct name, hope it will solve your problem.

I suffered from the same problem like you. Error occurs when you have any syntax error in your XAML file like misspell, missing tag, comma, lack of method/event handler or difficulties with references.

Exactly the same reason is when you are unable to add event or change any property in Property Window, but without any message or error. Check your syntax carefully.

Regards.

yes. I also face this problem, I fixed it. the problem is on x:Class="MainWindow". just like @Abdullah Said.

I had wrong namespace in XAML file after copying/pasting the code. Namespace in XAML and underlaying code must match.

如果 .cs 文件中已存在名称与 IDE 尝试创建的方法名称相同的方法,也会发生这种情况。

一旦我为按钮添加了一个名称,我就可以双击它,错误消失了,打开了事件处理程序的代码视图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM