tags:

views:

34

answers:

2

I see a Window1.xaml and its associated .cs file, which are the main window. Then there's also app.xaml and app.xaml.cs. First, what is the point of App? It seems to be empty.

I'm assuming I should put local variables to the Window on its cs file, and variables related to the whole program on App?

+1  A: 

You should MVVM. Start early, win often.

App.xaml and its code file are used for application-wide resources and code that needs to run on startup. You shouldn't be putting anything in app.xaml.cs unless you need to.

Will
could you elaborate on "code that needs to run on startup"?
they changed my name
@they well, as in code that needs to run on startup. Like.. uh... maybe you need to make sure a database exists before your app runs. So you override the OnStartup method, try to connect, and if not found create the database.
Will
+1  A: 

App.xaml and App.xaml.cs is your applicaton's entry point. Main() is actually auto-generated and hidden in the partial App class. In the XAML, you can set the Window that will initially display, so you are not forced to use Window1.

In reality, you don't need App.xaml either. In some applications I have made, I have instead opted to create an App.cs, where I manually create the Main() entry point and start the initial Window. If you don't need such fine control over your Main() method, I'd recommend keeping the default XAML method of startup. You can still subscribe to events in App's constructor using the XAML method, for startup code and handling unhandled exceptions.

You should put local variables with the class that needs them. Global variables are bad, but it's be better to use static classes and variables instead of inserting the unnecessary code in App.xaml.cs.

Will Eddins