Really new and learning C# and following along some training video from PluralSight. Great videos, except you cannot ask questions, of course and I am not understanding why what I am seeing is different that what his screen is displaying even though I typed exactly what he has.
Textbox is named “Output”. Initially, the actions were directly in the MainWindow constructor (which he explained is not good practice, so we moved it. Initially, this worked as it should:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Employee e1 = new Employee();
e1.Name = "Mike";
Employee e2 = new Employee();
e2.Name = "Miller";
Output.Text = e1.Name + " " + e2.Name;
}
}
}
This would display “Mike Miller” in the TextBlock.
However, when we moved it to this, all it says for the text is “TextBlock”
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();
e1.Name = "Mike";
Employee e2 = new Employee();
e2.Name = "Miller";
Output.Text = e1.Name.Length + " " + e2.Name.Length;
}
}
}
Am I missing something simple here?
Thanks!
As Nico Schertler stated, verify that you subscribed to Loaded event of Window:
In first case your code runs, because constructor of Window is called when Window is created. In second case, event handler is not called by default. You should subscribe to this event.