WPF编译错误

本文关键字:错误 编译 WPF | 更新日期: 2023-09-27 17:49:45

我正在尝试学习。net和WPF,我正在制作一个计算器类型的应用程序来学习一些框架。我的XAML代码看起来像这样:

<Window x:Class="CS_WPF_TEST.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>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBox Margin="4" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="5" Name="DisplayBox" IsReadOnly="True" />
        <Button Margin="4" Grid.Row="1" Grid.Column="0" Name="MC" Content="MC" Click="ClearMem" />
        <Button Margin="4" Grid.Row="1" Grid.Column="1" Name="MR" Content="MR" Click="RecallMem" />
        /snip/
        <Button Margin="4" Grid.Row="6" Grid.Column="3" Name="Plus" Content="+" Click="Plus" />
    </Grid>
</Window>

和这样的c#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 CS_WPF_TEST
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //snip
        //event handlers with declarations like following
        private void Handle(object sender, RoutedEventArgs e)
        {
            //code
        }
    }
}

<Button Margin="4" Grid.Row="2" Grid.Column="3" Name="Caret" Content="^" Click="Exponent" />
<Button Margin="4" Grid.Row="2" Grid.Column="4" Name="Sqrt" Content="Sqrt" Click="Sqrt" />

和相应的事件处理程序代码:

private void Exponent(object sender, RoutedEventArgs e)
{
    process_operation((a, b) => System.Math.Pow(a, b));
}
private void Seven(object sender, RoutedEventArgs e)
{
    process_input('7');
}

但是由于某些原因,我在exponent上没有错误,但是在Seven上我得到了如下错误:

c:'snip'CS_WPF_TEST'CS_WPF_TEST'MainWindow.xaml(34,49): error CS0102: The type 'CS_WPF_TEST.MainWindow' already contains a definition for 'Seven'
C:'snip'CS_WPF_TEST'CS_WPF_TEST'MainWindow.xaml.cs(264,22): (Related location)

是我的语法错了,还是有什么我不知道的,比如声明的顺序之类的?

WPF编译错误

看起来您正在为XAML控件和事件处理程序使用相同的名称。别这样。