简体   繁体   English

Visual Studio 2013 和 C# - 无法添加事件处理程序

[英]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.我正在处理 WPF 项目,在 Xaml 文件中制作所有内容后,我想开始处理我的 CS 文件。 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".现在,我试图使用属性中的那个小螺栓添加事件,但是每次我单击任何事件(例如,我在 XAML 中选择按钮并尝试添加 -> MyButton_Click)时,我都会收到一条弹出消息“无法添加事件处理程序”。

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.是的,@will 是对的,当我刚接触 WPF 时,我也遇到了同样的问题。 Please check your x:Class in your XAML file, it should include the namespace used in code behind.请检查您的 XAML 文件中的 x:Class,它应该包含后面代码中使用的命名空间。 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.例如,x:Class="MainWindow" 应该是类似 x:class="namespace.MainWindow" 的东西,如果它不起作用,请尝试重新启动 VS 或尝试使用正确的名称重新创建项目,希望它能解决您的问题问题。

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.当您的 XAML 文件中有任何语法错误(例如拼写错误、缺少标记、逗号、缺少方法/事件处理程序或引用困难)时,就会发生错误。

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".问题出在 x:Class="MainWindow" 上。 just like @Abdullah Said.就像@Abdullah Said 一样。

I had wrong namespace in XAML file after copying/pasting the code.复制/粘贴代码后,我在 XAML 文件中有错误的命名空间。 Namespace in XAML and underlaying code must match. XAML 中的命名空间和底层代码必须匹配。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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