我是Wpf和Caliburn的新手,我有以下情况,我希望得到一些帮助,我确信代码是正确的,但这不能按预期工作,详细说明
我的AppBootstrapper因此:
public class AppBootstrapper : BootstrapperBase
{
private CompositionContainer _container;
public AppBootstrapper()
{
this.Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
this.DisplayRootViewFor<ShellViewModel>();
}
protected override void Configure()
{
try
{
this._container = new CompositionContainer(new AggregateCatalog(new DirectoryCatalog(".", "*")));
var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(this._container);
this._container.Compose(batch);
}
catch (Exception ex)
{
this._log.Error(ex.ToString());
}
}
protected override object GetInstance(Type service, string key)
{
try
{
var contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : key;
var exports = this._container.GetExportedValues<object>(contract);
if (exports.Any())
{
return exports.First();
}
throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
}
catch (Exception ex)
{
if (ex is ReflectionTypeLoadException)
{
var typeLoadException = ex as ReflectionTypeLoadException;
var loaderExceptions = typeLoadException.LoaderExceptions;
if (loaderExceptions != null)
{
this._log.Error(loaderExceptions.First().ToString());
}
}
throw;
}
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return this._container.GetExportedValues<object>(AttributedModelServices.GetContractName(service));
}
protected override void BuildUp(object instance)
{
this._container.SatisfyImportsOnce(instance);
}
}
我的ShellViewModel如下(我删除了一些代码以保持简短)
[Export(typeof(ShellViewModel))]
public class ShellViewModel : Conductor<IScreen>,
IHandle<NavigationEvent>,
IHandle<WindowCloseEnabledEvent>,
IShell,
IShellViewModel
{
private readonly IEventAggregator _eventAggregator;
[ImportingConstructor]
public ShellViewModel(IEventAggregator eventAggregator)
{
this._eventAggregator = eventAggregator;
}
[Import(typeof(LoginViewModel))]
public Lazy<IScreen> LoginViewModel { get; set; }
public void Loaded()
{
this._eventAggregator.Subscribe(this);
this.ActivateItem(this.LoginViewModel.Value);
}
}
我在ShellView.xaml中有一个触发上面的Loaded方法的事件触发器。
我还有一个LoginViewModel和LoginView(注意这些是在项目的ViewModels和Views文件夹中。
LoginViewModel的重要部分是
[Export(typeof(LoginViewModel))]
public class LoginViewModel : Screen, IAnimatableViewModel, ILoginViewModel
{
protected override void OnViewLoaded(object view)
{
this._view = view;
IoC.Get<ShellViewModel>().DisableClose = true;
base.OnViewLoaded(view);
}
}
此时LoginView.xaml只显示一些文字。
我使用MEF作为DI容器,问题是当我运行应用程序时加载了ShellView,它应该将LoginView加载到其中但是LoginView不加载(或不显示)
如果有人能提供帮助,我们将不胜感激。