[英]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.