You could do it like this.
First go into App.xaml
and remove this line StartupUri="MainWindow.xaml"
to prevent WPF from automatically showing the MainWindow
.
Next right click on App.xaml
and choose View Code
to open up App.xaml.cs
. Inside this file we need to to override the OnStartup
event.
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
}
Inside OnStartup
we can then instantiate our MainWindow
and show it.
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mw = new MainWindow();
mw.Show();
}
And now we can use this to load an alternative Constructor
that we can use to pass on more information.
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mw = new MainWindow(5);
mw.Show();
}
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
}
public MainWindow(int number) : base()
{
}
I prefer to chain my constructors, but it's of course not a requirement by any means.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…