tags:

views:

137

answers:

2

What would be the best way to save the window position and size in a WPF app?

Currently, I'm saving the window size and position of a WPF App. Here are the events I handle:

  1. SourceInitialized : The saved info is loaded on to the window
  2. WindowClosing : The current info is saved to the backing store

(I copied this from an example).

The problem is, when the window is minimized and restored, the settings from the last WindowClosing is retrieved.

Now, the StateChanged event fire AFTER the window has minimized, so it does not seem to be what i need.

Thanks

+4  A: 

Do yourself and your users a favor and use the LocationChanged event and the SizeChanged event to save the settings at that time. There's nothing more annoying than an application that gets amnesia if the process exits abnormally and settings don't get saved (cough...explorer...cough...)

Then just check to make sure the WindowState == Normal before saving the settings. Obviously its pointless to save the position of a minimized or maximized window.

As for when to load the settings, well that you can just do in the constructor after the InitializeComponent call or you can use the Initialized event. No real reason to use the SourceInitialized event unless you are doing something with the HWND directly which shouldn't be necessary.

Josh Einstein
A: 

Are you doing this via databinding? That is the way I do my window sizing and position. I typically have a UserConfig.xml file that is saved in the Users Profile. Then I create elements in there as I databind them in the program. I have the Application.xaml resource dictionary refer to that file, and all of the settings I want set to XPaths inf the XML. Then I just save the in memory xml document on exit. Only one event to handle, no mess, no fuss.

And you can expand it to encompass as many settings as you like in regard to the UI. Adding plumbing settings is a little more difficult, but not terribly so.

OffApps Cory