wpf 您是否缺少 using 指令或程序集引用?C# 中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24185719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:54:05  来源:igfitidea点击:

are you missing a using directive or an assembly reference? Error in C#

c#wpf

提问by jason

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”的定义,并且找不到接受“HelloWPFApp.MainWindow”类型的第一个参数的扩展方法“RadioButton1_Checked”(您是否缺少 using 指令或程序集引用?)

Can you tell me what the problem is?

你能告诉我是什么问题吗?

回答by cvraman

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 或在后面的代码中定义一个事件处理程序。

回答by Lamourou abdallah

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