我正在尝试创建一个非常简单的C#程序.
这是xaml文件:
<Window x:Class="HelloWPFApp.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>
<TextBlock HorizontalAlignment="Left" Margin="104,72,0,0" TextWrapping="Wrap" Text="Select a message option and then choose the Display button" VerticalAlignment="Top"/>
<RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top" Checked="RadioButton1_Checked"/>
<RadioButton x:Name="RadioButton2" Content="Goodbye;" HorizontalAlignment="Left" Margin="342,138,0,0" VerticalAlignment="Top"/>
<Button Content="Display" HorizontalAlignment="Left" Margin="211,208,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是.cs文件:
using....
namespace HelloWPFApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (RadioButton1.IsChecked == true)
MessageBox.Show("Hello");
else
MessageBox.Show("Goodbye");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
"HelloWPFApp.MainWindow"不包含关于"RadioButton1_Checked"的定义和没有扩展方法"RadioButton1_Checked"接受型的第一参数"HelloWPFApp.MainWindow"可以找到(是否缺少using指令或程序集引用?)
你能告诉我这是什么问题吗?
您已在xaml代码中定义了事件处理程序
<RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top" Checked="RadioButton1_Checked"/>
Run Code Online (Sandbox Code Playgroud)
但是你没有在你的代码隐藏中定义相同的东西.这就是导致问题的原因.从上面的单选按钮中删除Checked Event,或者在代码后面定义一个事件处理程序.
| 归档时间: |
|
| 查看次数: |
16272 次 |
| 最近记录: |