Hi, 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
IWindowManager
only hasShowDialog
,ShowPopup
andShowWindow
methods? - 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?
Many thanks, additional info below:
My bootstrapper:
publicclass SimpleInjectorBootstrapper : Caliburn.Micro.Bootstrapper
{private Container container;protectedoverridevoid 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>();
}protectedoverrideobject GetInstance(Type serviceType, string key)
{returnthis.container.GetInstance(serviceType);
}protectedoverride IEnumerable<object> GetAllInstances(Type serviceType)
{returnthis.container.GetAllInstances(serviceType);
}protectedoverridevoid 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:
publicclass LoginViewModel : PropertyChangedBase, ILoginViewModel
{privatereadonly ITransportClient transportClient;privatereadonly IWindowManager windowManager;privatestring connectionStatus;public LoginViewModel(ITransportClient transportClient, IWindowManager windowManager)
{this.transportClient = transportClient;this.windowManager = windowManager;this.transportClient.ConnectionEvent += new TransportConnectionEventHandler(UpdateStatusHandler);
}publicvoid Login()
{// set from view, to be done via property, implement latervar 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?
}publicvoid UpdateStatusHandler(string status)
{this.ConnectionStatus = status;
}publicstring ConnectionStatus
{get
{returnthis.connectionStatus;
}set
{this.connectionStatus = value;
NotifyOfPropertyChange(() => ConnectionStatus);
}
}
}