Caliburn Micro Part 6: The Window Manager

Caliburn usually takes care of creating the main window for use, but what if we wanted to dynamically create a popup window our self? To do this we can use the provided WindowManager class.

Creating a popup window

IWindowManager manager = new WindowManager();
manager.ShowDialog( myViewModel, null, null );

Simple put the code above creates a new modal window which shows the view associated with the supplied view model (myViewModel).

Customising the window

We can do this by passing an array of settings to the ShowDialog function, to do this we use an ExpandoObject which is interpreted at runtime ( this means that you won’t get any intelli sense or error checking). You can add any property to the ExpandoObject that would be settable on a wpf window.

dynamic settings = new ExpandoObject();
settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
settings.ResizeMode = ResizeMode.NoResize;
settings.MinWidth = 450;
settings.Title = "My New Window";
settings.Icon = new BitmapImage( new Uri( "pack://application:,,,/MyApplication;component/Assets/myicon.ico" ) );

IWindowManager manager = new WindowManager();
manager.ShowDialog( myViewModel, null, settings );

Notes

The new window does not have to be modal, there are other functions on the window manager for showing a non modal window and a popup.

That’s it for this tutorial join us next time for a look at using Dependency Injection.

Leave a comment