我确定这个问题以前应该被问过,但是我无法确切找到我想要的东西;
请考虑以下内容:
- Solution
-- Class Library Project [Caliburn.Micro] Referenced
--- [Models] Folder
---- LogEntryModel.cs
-- WPF GUI Project [Caliburn.Micro] Referenced
--- [ViewModels] Folder
--- [Views] Folder
---- LogEntryView.xaml
---- ShellView.xaml
所以,我有2个项目,一个项目包含模型,一个项目包含ViewModels和Views;这是我的引导程序:
public class AppBootstrapper : BootstrapperBase
{
private CompositionContainer container;
public AppBootstrapper()
{
Initialize();
}
protected override void BuildUp(object instance)
{
this.container.SatisfyImportsOnce(instance);
}
/// <summary>
/// By default, we are configured to use MEF
/// </summary>
protected override void Configure()
{
var catalog =
new AggregateCatalog(
AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());
this.container = new CompositionContainer(catalog);
var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(this.container);
batch.AddExportedValue(catalog);
this.container.Compose(batch);
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return this.container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
}
protected override object GetInstance(Type serviceType, string key)
{
var contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : 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));
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
var startupTasks =
GetAllInstances(typeof(StartupTask))
.Cast<ExportedDelegate>()
.Select(exportedDelegate => (StartupTask)exportedDelegate.CreateDelegate(typeof(StartupTask)));
startupTasks.Apply(s => s());
DisplayRootViewFor<IShell>();
}
}
现在,当我尝试使用绑定到列表框的LogEntryModel时,我显示Cannot find view for Project.Models.LogEntryModel.
如果要添加新规则来链接viewModel和视图,则必须使用ViewLocator:
一些样品:
ViewLocator.NameTransformer.AddRule(@"ViewModel", @"ViewX");
ViewLocator.AddNamespaceMapping("Cockpit.Core.Plugins.Plugins.Properties", "Cockpit.General.Properties.Views");