如何处理按钮单击XAML?

时间:2015-11-26 22:31:16

标签: c# wpf xaml

我是wpf项目的初学者。

我创建了一个wpf项目,下面的代码是我的“MainWindow.xaml”文件。

<Window x:Class="Test_Xaml.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Add" Click="Button_Click" HorizontalAlignment="Left" Margin="158,127,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.507,2">
        </Button>
        <Button Content="Remove" HorizontalAlignment="Left" Margin="158,173,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.027,-0.091">
        </Button>
        <ComboBox HorizontalAlignment="Left" Margin="276,93,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="-0.2,0.286"/>
        <TextBlock HorizontalAlignment="Left" Margin="138,90,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="24" Width="113"/>
    </Grid>
</Window>

这是我的“MainWindow.xaml.cs”文件内容。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

    }
}

但是我收到了这个错误。

Error   1   'Test_Xaml.MainWindow' does not contain a definition for 'Button_Click' and no extension method 'Button_Click' accepting a first argument of type 'Test_Xaml.MainWindow' could be found (are you missing a using directive or an assembly reference?)   C:\Users\Amir\Documents\Visual Studio 2013\Projects\Test Xaml\Test Xaml\MainWindow.xaml 6   108 Test Xaml

请帮助我,我浪费了12个小时来编译我的代码!

2 个答案:

答案 0 :(得分:3)

哟有错误的命名空间。将Test_Xaml.MainWindow更改为test02.MainWindow

<Window x:Class="test02.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Add" Click="Button_Click" HorizontalAlignment="Left" Margin="158,127,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.507,2">
        </Button>
        <Button Content="Remove" HorizontalAlignment="Left" Margin="158,173,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.027,-0.091">
        </Button>
        <ComboBox HorizontalAlignment="Left" Margin="276,93,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="-0.2,0.286"/>
        <TextBlock HorizontalAlignment="Left" Margin="138,90,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="24" Width="113"/>
    </Grid>
</Window>

答案 1 :(得分:3)

检查您的命名空间。在XAML中,您有 Test_Xaml .MainWindow,并且在 test02

后面的代码中