我在wpf项目中遇到使用Caliburn.Micro
的困难:执行任何视图模型的代码,但窗口永远不会出现。因此,当窗口保持不可见时,OnViewLoaded
可以显示MessageBox。
这是我的引导程序:
public class Bootstrapper : BootstrapperBase
{
/// <summary>
/// IoC container for dependency injection
/// </summary>
private readonly SimpleContainer _container = new SimpleContainer();
private static Bootstrapper bootstrapper;
public Bootstrapper()
{
Initialize();
ConventionManager.AddElementConvention<PasswordBox>(ZBMS.PasswordHelper.BoundPasswordBox.BoundPasswordProperty, "Password",
"PasswordChanged");
}
protected override void Configure()
{
//Callback for redirecting pressed keys to the corresponding viewmodel
MessageBinder.SpecialValues.Add("$pressedkey", (context) =>
{
var keyArgs = context.EventArgs as KeyEventArgs;
return keyArgs?.Key;
});
// reset settings when debugger is attached (vs obviously)
if (Debugger.IsAttached)
Settings.Default.Reset();
_container.Singleton<IEventAggregator, EventAggregator>();
_container.Singleton<IWindowManager, WindowManager>();
//_Container.Singleton<ISettingsManager, NETSettingsManager>();
_container.PerRequest<LoadingScreenViewModel>();
}
protected override void OnStartup(object sender, StartupEventArgs e)
=> DisplayRootViewFor<LoadingScreenViewModel>();
protected override object GetInstance(Type service, string key)
{
var instance = _container.GetInstance(service, key);
if (instance != null)
return instance;
throw new InvalidOperationException("Could not locate any instances.");
}
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[]
{
Assembly.GetExecutingAssembly()
};
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}