![](/img/trans.png)
[英]MouseUp event handler not working after MouseDown event is called on the same button WPF C#
[英]WPF C# Button Event Handler Not Working
我在让事件处理程序为按钮工作时遇到问题。 我正在使用Visual Studio2015。我的代码和错误如下:
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"/>
后面的代码:
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");
}
}
}
错误:CS1061'MainWindow'不包含'Button1_Click'的定义,并且找不到扩展方法'Button1_Click'接受类型为'MainWindow'的第一个参数(您是否缺少using指令或程序集引用?)
每当我添加按钮并单击事件处理程序部分时,这就是我看到的内容:该文档项目没有任何代码隐藏文件。 在添加事件处理程序之前,添加一个代码隐藏文件和一个类定义。
任何帮助,将不胜感激。
它适用于以下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>
以及文件后面的以下代码
/// <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");
}
}
正是您所拥有的。 您是否尝试过重建解决方案?
听起来好像事件未在页面中注册,单击您的按钮并检查事件选项卡以查看事件是否已注册...检查此图像: https : //postimg.org/image/5q6nvac7b/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.