I am using Caliburn Micro and have a login view model which is displayed on startup.
I am using a separate class which handles all of the connection to the server and provides simple call back events to the ViewModel, this is the ITransportClient.
The users enters their credentials, hits login, and the dialog shows the connection process along a number of states (connecting, validating username, downloading configuration). In the background the Login ViewModel will have called ITransportClient.Login().
If logged in OK and all steps are completed, the form should then close and the main window ViewModel should open up. If the credentials are incorrect or a problem downloading settings, an error should be displayed and the login form remain.
If connection is lost to the server (indicated via ITransportClient event), then the application should attempt to reconnect a number of times, and if the server remains offline for a configurable period of time then the login window should be displayed again.
- How would I best handle the switching between the login dialog and the main window as per the above flow?
- How can the login ViewModel close itself, I see
IWindowManageronly hasShowDialog,ShowPopupandShowWindowmethods? - What is the best way to separate the above out, allowing the login window to be closed external to the Login ViewModel, and for the login window to be displayed when the user logs out the main window? Should this be done in the bootstrap, or should a separate ViewModel shell be created for this?
My bootstrapper:
public class SimpleInjectorBootstrapper : Caliburn.Micro.Bootstrapper
{
private Container container;
protected override void Configure()
{
this.container = new Container();
this.container.Register<IWindowManager, WindowManager>();
this.container.Register<IEventAggregator, EventAggregator>();
this.container.Register<IAppViewModel, AppViewModel>();
this.container.Register<ILoginViewModel, LoginViewModel>();
this.container.RegisterSingle<ITransportClient, Transport.WCF.TransportClient>();
}
protected override object GetInstance(Type serviceType, string key)
{
return this.container.GetInstance(serviceType);
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return this.container.GetAllInstances(serviceType);
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
base.OnStartup(sender, e);
var loginViewModel= this.container.GetInstance<ILoginViewModel>();
var windowManager = this.container.GetInstance<IWindowManager>();
windowManager.ShowWindow(loginViewModel);
}
}
My LoginView model is below:
public class LoginViewModel : PropertyChangedBase, ILoginViewModel
{
private readonly ITransportClient transportClient;
private readonly IWindowManager windowManager;
private string connectionStatus;
public LoginViewModel(ITransportClient transportClient, IWindowManager windowManager)
{
this.transportClient = transportClient;
this.windowManager = windowManager;
this.transportClient.ConnectionEvent += new TransportConnectionEventHandler(UpdateStatusHandler);
}
public void Login()
{
// set from view, to be done via property, implement later
var username = "test";
var password = "test";
var result = this.transportClient.Login(username, password);
// if result is ok, we should close our viewmodel, however we
// cant call IWindowManager.Close(this) as only show methods exist
// perhaps this is better handled elsewhere?
}
public void UpdateStatusHandler(string status)
{
this.ConnectionStatus = status;
}
public string ConnectionStatus
{
get
{
return this.connectionStatus;
}
set
{
this.connectionStatus = value;
NotifyOfPropertyChange(() => ConnectionStatus);
}
}
}
CM uses the
IWindowManagerinterface to provide the contract for window management that CM can understand. The implementation itself is a UI specific thing – e.g. Telerik controls vs Microsoft standard controls = different.IWindowManageris implemented in CM asWindowManagerand this manager contains a child class calledWindowConductorwhose job is to handle the events from theWindowcontrol itself and pass calls to the window (its the glue between the viewmodel and views container)Take a look:
http://caliburnmicro.codeplex.com/SourceControl/changeset/view/35f41b2f9113#src%2fCaliburn.Micro.Silverlight%2fWindowManager.cs
This conductor manages the window for you – if you look at the implementation, you can see that it checks for the presence of certain interfaces such as
IActivate,IDeactivateandIGuardClose. If you implement these interfaces, your window gains more lifecycle features.Inheriting from
Screeninstead ofPropertyChangedBaseis one way to get these interface implementations for free, you also get implementation ofIViewAwarewhich provides automatic view caching and a useful method for getting a reference to the viewOnce you have inherited from this class or implemented the interfaces, you can then call methods to close the window via the VM (such as
TryClose). TheWindowConductorwill take care of passing the neccessary calls to the window control, you can even prevent the window from closing using theIGuardCloseinteface which allows you to cancel the close operation.I assume you already have a handle on
Actionsin CM, since you seem to have a login method on your VM.Now this is more of a question in an answer – do you need to go the popup window route? You could just inherit from
Conductor<T>.Collections.OneActiveand callActivateItem(yourViewModel)(Tcan beIScreen). Your login viewmodel will be deactivated automatically when the new item is activated, or you can open a login failed viewmodel or something of that ilk. Whilst this doesn’t use a separate window, the implementation is quite simple, just bind aContentControltoActiveItemon your conductor viewmodelHave a look here for details (look at the Simple MDI section and surrounding):
http://caliburnmicro.codeplex.com/wikipage?title=Screens%2c%20Conductors%20and%20Composition&referringTitle=Documentation