[英]are you missing a using directive or an assembly reference? Error in C#
I'm trying to create a very simple C# program. 我正在尝试创建一个非常简单的C#程序。
Here is the xaml file : 这是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>
Here is the .cs file : 这是.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");
}
}
}
And this is the error I get : 这是我得到的错误:
'HelloWPFApp.MainWindow' does not contain a definition for 'RadioButton1_Checked' and no extension method 'RadioButton1_Checked' accepting a first argument of type 'HelloWPFApp.MainWindow' could be found (are you missing a using directive or an assembly reference?)
'HelloWPFApp.MainWindow'不包含'RadioButton1_Checked'的定义,并且没有扩展方法'RadioButton1_Checked'可以找到接受类型'HelloWPFApp.MainWindow'的第一个参数(你是否缺少using指令或汇编引用?)
Can you tell me what the problem is? 你能告诉我这是什么问题吗?
You have defined an event Handler in your xaml code 您已在xaml代码中定义了事件处理程序
<RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top" Checked="RadioButton1_Checked"/>
But you have not defined the same in your code-behind. 但是你没有在你的代码隐藏中定义相同的东西。 That's what causing the problem.
这就是导致问题的原因。 Either remove the Checked Event from the above radio button or define an event handler in your code behind.
从上面的单选按钮中删除Checked Event,或者在代码后面定义一个事件处理程序。
define a method for the event radio button checked, add this to MainWindow.xaml.cs 定义选中事件单选按钮的方法,将其添加到MainWindow.xaml.cs
void RadioButton1_Checked(object sender, RoutedEventArgs e)
{
//add your job here
}
or replace this line 或者替换这一行
<RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top" Checked="RadioButton1_Checked"/>
by this one 通过这个
<RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.