gpt4 book ai didi

c# - 我如何处理 XAML 中的按钮单击?

转载 作者:太空宇宙 更新时间:2023-11-03 19:02:20 29 4
gpt4 key购买 nike

我是 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 个小时来编译我的代码!

最佳答案

您的命名空间有误。将 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>

关于c# - 我如何处理 XAML 中的按钮单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33947866/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com