WPF 编译错误

发布于 2024-10-31 10:36:15 字数 3084 浏览 0 评论 0原文

我正在尝试学习.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');
}

但由于某种原因,我在指数上没有错误,但在七上遇到如下错误:

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)

我的语法是否错误,或者有什么我不知道的事情,比如声明的顺序什么的?

I'm trying to learn .NET and WPF, and I'm making a calculator type application to learn some of the framework. I have XAML code that looks like this:

<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>

And C# Code like this:

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" />

And the corrosponding Event Handler Code:

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');
}

But for some reason, I have no errors on exponent, but I am getting errors like the following on 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)

Is my syntax wrong, or is there something I don't know about, like the order things are declared in or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒一口袋星星 2024-11-07 10:36:15

您的 XAML 控件和事件处理程序似乎使用了相同的名称。不要那样做。

It looks like you're using the same name for your XAML controls and your event handlers. Don't do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文