0

Sorry for confusing title =D

In WindowsForms I have Main method, which I can use to do something before creating or displaying main form. For example, processing command line parameters, showing splash window and preloading configuration, having several windows shown one after each other, main loop (when there is a repeatable working sequence, then Main is pretty good place to put there do{}while), etc.

Which is the best analog for such Main method in WPF?

I tried to use Application.Startup event, but it has some issues.

I could think of creating wpf application programmatically somehow (then I should be unbound from xaml autogenerated Main and can have my own). But this all sounds too complicated. I probably missing something very simple.

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Have a read of http://stackoverflow.com/questions/7955663/how-to-build-splash-screen-in-windows-forms-application and http://stackoverflow.com/questions/2455703/splash-screen-example – Paul Zahra Mar 19 '14 at 10:35
  • 5
    I know it's off-topic, but I just like the existential question title! – Sean Mar 19 '14 at 10:38

2 Answers2

4

Moving from WinForms development to WPF development can involve a little bit of a change of mindset. I would look into WPF and MVVM, there are plenty of resources on the web.

After that, you should start looking at the various frameworks which build on this, such as Prism, Caliburn.Micro, MVVMLight etc, which all have various tutorials and documentation around building WPF applications with splash screens and composite windows.

For what it's worth, protected override void OnStartup in App.xaml.cs is where you'd want to "do something, before anything", like, show a splash screen before loading your main screen.

Stuart Grassie
  • 3,043
  • 1
  • 27
  • 36
  • Thanks for suggestion, but override `OnStartup` doesn't works (same as subscribing to `Startup` event), see my issue in question if you want. I am just started with studying WPF and MVVM, probably I am making some mistakes (not planning to use any side frameworks yet, until I really find good reasoning for a beginner to go straight into, to example, Prism). My question is not how to make splash window, but how to organize it. `Main` is generated, event/override is not working good, so where to put my code before anything, which will not cause problems to the application itself? – Sinatr Mar 19 '14 at 16:02
0

Solution is ... do everything in the Main.

For this I have to disable auto-generating Main (by setting App.xaml property BuildAction to Page, instead of default ApplicationDefinition) and then I can create own Main inside Application class:

public partial class App : Application
{
    [STAThread]
    public static void Main()
    {
    }
}

There I have full control over when and how to display windows, do initialization, deinit and such.

Sinatr
  • 20,892
  • 15
  • 90
  • 319