Make WindowManager remember where it opened a window

Topics: Framework Services
Mar 6, 2012 at 6:27 PM

I have a application with a lot of tool windows. Is there a way to make the WindowManager remember where it opened a specifc window the last time (e.g. by keeping the windows it created alive after closing them)? How is something like this ideally done in Caliburn Micro?

Mar 6, 2012 at 7:02 PM

I would consider creating a service, registered in your IoC container, which can be used by windows to track significant pieces of data like position, size, etc. Build the service such that any time a value is changed, it writes its data to isolated storage.

Mar 6, 2012 at 7:08 PM
Edited Mar 6, 2012 at 7:09 PM

Good idea. What would you use to identify a toolwindow (e.g. ColorPicker, vs. FontsDialog vs. PropertyDialog)?

Mar 6, 2012 at 7:16 PM

You could plumb this in via an attached property if you like. So that you have a way to pass the window instance along with a unique key to the service. When attaching the property, you can lookup the key and restore values if found. Then you can wire for move, resize, etc. events and have the code automatically push those values into the service. I'm not sure if this is the best way to do it, but it should be pretty re-usable and pretty no-hassle to add new windows. Does that answer the question?

Mar 6, 2012 at 7:27 PM
Edited Mar 6, 2012 at 7:43 PM

Well almost:

Because the WindowManager creates the Windows and fills them with a View using the ViewLocator dynamically, that unique key must be attached to my Views (in that case I need to make a connection between View and Window, probably by walking the visual tree) or it could come from the ViewModel (e.g. object IWindowContext.Context { get; }). Not quite sure which is the better approach.

I am also not so sure how I would set the window's position before they are shown by the WindowManager. Wouldn't I need to hook into the WindowManager's window creation?

Mar 6, 2012 at 7:54 PM

You can get the window from the Parent property of your UserControl pretty easily. You could also wire for the loaded/activated or some event of the window and set the values when that fires. No need to mess with altering the WindowManager.

Mar 6, 2012 at 8:38 PM
Edited Mar 6, 2012 at 8:38 PM

Ok, got it! Have all the pieces together and it seems pretty straightforward.

Mar 6, 2012 at 9:16 PM

Let me know how it turns out. If you come up with an alternative solution, be sure to post it here for future readers.