问题
We have an application that shall support a "batch mode". So we want to make it callable from the console, just like devenv.exe.
The application has a WPF user interface, which is the default interface for most users. We use caliburn.micro with a bootstrapper for the mainwindow.
Think we should have a different bootstrapper for that. (Or no bootstrapper at all)
- How can we "choose" what the environment is?
- Is there any way to do anything before the bootstrapper begins his job?
回答1:
If I understand your question correctly then you want to run the application either in console mode (from the CMD) or in UI mode with the WPF interface, correct ?.
Think we should have a different bootstrapper for that. (Or no bootstrapper at all)
You can keep the bootstrapper, but you may need to modify it a bit so it looks like this:
public class AppBootstrapper : BootstrapperBase
{
public AppBootstrapper()
{
Start(); // THIS IS WHAT CAUSES THE FRAMEWORK TO INITIALIZE
}
protected override void Configure()
{
// DIFFERENT CONFIGURATION GOES HERE
}
protected override object GetInstance(Type service, string key)
{
// DI CONTAINER RELATED CONFIGURATION
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
// DI CONTAINER RELATED CONFIGURATION
}
protected override void BuildUp(object instance)
{
// DI CONTAINER RELATED CONFIGURATION
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
// ANY CUSTOM BEFORE-START CUSTOMIZATION OR PROCESSING CAN TAKE PLACE HERE
DisplayRootViewFor<SPECIFIY_ROOT_VIEW_MODEL_HERE>(); // THIS IS WHAT DISPLAYS THE MAIN WINDOW, IF YOU DON'T CALL THIS NO UI WILL BE SHOWN
}
}
How can we "choose" what the environment is?
In the OnStartup()
override shown above in the bootstrapper and before the DisplayRootViewFor
call, you can obtain a list of command line arguments by calling Environment.GetCommandLineArgs() and then perhaps you could ask users who want to work in batch mode to specify an argument, and based on that you could or could not call DisplayRootViewFor
to show the main window.
Is there any way to do anything before the bootstrapper begins his job?
Yes, you can do that in the OnStartup()
method preferably or if you wish in the Configure()
method.
Note: Both Configure()
and OnStartup()
won't get called unless you call Start()
in the constructor.
来源:https://stackoverflow.com/questions/17940983/how-to-have-a-application-with-caliburn-for-both-console-and-wpf