简体   繁体   English

WPF C#按钮事件处理程序不起作用

[英]WPF C# Button Event Handler Not Working

I am having a problem getting and event handlers to work for a button. 我在让事件处理程序为按钮工作时遇到问题。 I am using Visual Studio 2015. My code and error is below: 我正在使用Visual Studio2015。我的代码和错误如下:

XAML: XAML:

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="500" Width="983.334">
.
.
.
    <Button x:Name="Button1" Content="Database" HorizontalAlignment="Left" 
            Margin="10,427,0,0" VerticalAlignment="Top" Width="99" 
            Click="Button1_Click"/>

Code behind: 后面的代码:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Test");
        }
    }
}

Error: CS1061 'MainWindow' does not contain a definition for 'Button1_Click' and no extension method 'Button1_Click' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?) 错误:CS1061'MainWindow'不包含'Button1_Click'的定义,并且找不到扩展方法'Button1_Click'接受类型为'MainWindow'的第一个参数(您是否缺少using指令或程序集引用?)

Whenever I add a button and click on the event handler section this is what I see: The document item had no code-behind file. 每当我添加按钮并单击事件处理程序部分时,这就是我看到的内容:该文档项目没有任何代码隐藏文件。 Add a code-behind file and a class definition before adding event handlers. 在添加事件处理程序之前,添加一个代码隐藏文件和一个类定义。

Any help would be appreciated. 任何帮助,将不胜感激。

It works for me for the following XAML 它适用于以下XAML

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="500" Width="983.334">
    <Button x:Name="Button1" Content="Database" HorizontalAlignment="Left" 
        Margin="10,427,0,0" VerticalAlignment="Top" Width="99" Click="Button1_Click"/>
</Window>

and the following code behind file 以及文件后面的以下代码

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Test");
        }
    }

which is exactly what you have. 正是您所拥有的。 Have you tried rebuilding the solution? 您是否尝试过重建解决方案?

听起来好像事件未在页面中注册,单击您的按钮并检查事件选项卡以查看事件是否已注册...检查此图像: https : //postimg.org/image/5q6nvac7b/

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

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