【问题标题】:are you missing a using directive or an assembly reference? Error in C#您是否缺少 using 指令或程序集引用? C#中的错误
【发布时间】:2014-06-12 13:30:24
【问题描述】:

我正在尝试创建一个非常简单的 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>

这是 .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");

        }
    }
}

这是我得到的错误:

'HelloWPFApp.MainWindow' 不包含 'RadioButton1_Checked' 并且没有扩展方法 'RadioButton1_Checked' 接受“HelloWPFApp.MainWindow”类型的第一个参数可能是 找到(您是否缺少 using 指令或程序集引用?)

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

【问题讨论】:

  • 您的 XAML 包含 Checked="RadioButton1_Checked" 而您的代码隐藏类中没有该方法。

标签: c# wpf


【解决方案1】:

您已经在您的 xaml 代码中定义了一个事件处理程序

 <RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top" Checked="RadioButton1_Checked"/>

但是您没有在代码隐藏中定义相同的内容。这就是造成问题的原因。从上面的单选按钮中删除 Checked Event 或在后面的代码中定义一个事件处理程序。

【讨论】:

    【解决方案2】:

    为选中的事件单选按钮定义一个方法, 将此添加到 MainWindow.xaml.cs

    void RadioButton1_Checked(object sender, RoutedEventArgs e)
    {
        //add your job here
    }
    

    或替换此行

    <RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top" Checked="RadioButton1_Checked"/>
    

    这个

    <RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      相关资源
      最近更新 更多