This code doesn't show the Window, it just closes automatically. Why is this happening?
class Program { [STAThread] static void Main(string[] args) { var window = new MainWindow(); window.ShowDialog(); } } I know that you can fix it adding a new Application.Run(window) but I would like to know why it has this behavior and why you have to invoke the Run method over the window instance.
EDIT:
Extending the previous question, I've noticed that this code will work:
- Create a new WPF Application.
- Go to App.xaml and delete the StartupUri
Modify the App.xaml.cs overriding the method OnStartup
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var window = new MainWindow(); window.Show(); } }
With this, the window remains open. What's going on under the hood?
Application.Runthere is no message loop.