Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

I'm trying to create a very simple C# program.

Here is the xaml file :

<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 :

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?)

Can you tell me what the problem is?

like image 347
jason Avatar asked Jun 12 '14 13:06

jason


1 Answers

You have defined an event Handler in your xaml code

 <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.

like image 114
cvraman Avatar answered Sep 28 '22 03:09

cvraman