简体   繁体   English

你错过了使用指令或程序集引用吗? C#出错

[英]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.

相关问题 C#您是否缺少using指令或程序集引用? - C# Are you missing a using directive or an assembly reference? c#错误1找不到类型或名称空间名称”(是否缺少using指令或程序集引用?) - c# Error 1 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?) 错误:您是否缺少使用指令或程序集引用的信息? - error:are you missing using directive or a assembly reference? 在C#vscode中使用指令汇编引用错误丢失 - missing using directive assembly reference error in c# vscode C# 错误:错误 CS0246 找不到类型或命名空间名称“”(您是否缺少 using 指令或程序集引用?) - C# Error: Error CS0246 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?) 如何解决C#错误“找不到类型或名称空间名称&#39;[x]&#39;(您是否缺少using指令或程序集引用?)” - How to fix C# error “The type or namespace name '[x]' could not be found (are you missing a using directive or an assembly reference?)” C# 错误 CS024 找不到类型或命名空间名称“Form1”(您是否缺少 using 指令或程序集引用?) - C# Error CS024 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?) C# 错误 CS0246 找不到类型或命名空间名称“Socket”(您是否缺少 using 指令或程序集引用) - C# error CS0246 The type or namespace name 'Socket' could not be found (are you missing a using directive or an assembly reference) 您是否在创建不同类的Obect时缺少c#中的using指令或程序集引用 - Are you missing a using directive or an assembly reference in c# on creating Obect of different class 找不到类型或名称空间名称“ MySql”(您是否缺少using指令或程序集引用?)c#VS 2013 - The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference?) c# VS 2013
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM