Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MainWindow_Loaded isn't triggered on my WPF Application

I'm currently following the Pluralsight C# Fundamentals: Part 1 and on the Classes and Objects section the video instructs me to create a new WPF Applicaiton in Visual Studio and fill in the code. This results in the following.

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Employee e1 = new Employee("Ash");

            Employee e2 = new Employee("Lee");

            Output.Text = e1.Name + "" + e2.Name;
        }
    }
}

Employee is basically a dummy class which has been defined with a single instance variable Name in order to demonstrate how constructors work.

There is also a TextBlock in my MainWindow.xaml called Output which I am trying to update on the last line of the code.

Initially I had the code contained in MainWindow_Loaded in the constructor MainWindow, the tutorial says this is bad practice and it should look like my first code snippet.

However this stops the application working. My question is what is the problem?

Is the tutorial outdated? Am I running the wrong version? Was it changed in Beta?

Thanks.

like image 863
Ashley Avatar asked Nov 13 '11 00:11

Ashley


Video Answer


1 Answers

Make sure the Loaded event mapped to MainWindow_Loaded in the XAML for MainWindow.

Edit: Moving my comment below into the answer, as it seems to be more helpful:

open up MainWindow.xaml (not MainWindow.xaml.cs), click on the window (make sure you don't have one of the controls selected), open the properties box (i believe F4 will do that), click on the events tab in the properties box, find Loaded and make sure that is mapped to MainWindow_Loaded (if it is blank you should be able to select your already existing one)

like image 123
BlackICE Avatar answered Sep 28 '22 08:09

BlackICE