Я уверен, что этот вопрос должен был задаваться раньше, но я не могу найти именно то, что ищу;
Учтите следующее:
- Solution
-- Class Library Project [Caliburn.Micro] Referenced
--- [Models] Folder
---- LogEntryModel.cs
--- [ViewModels] Folder
---- LogEntryViewModel.cs
---- ShellViewModel.cs
-- WPF GUI Project [Caliburn.Micro] Referenced
--- [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 config = new TypeMappingConfiguration
{
DefaultSubNamespaceForViews = "WPFGUI.Views",
DefaultSubNamespaceForViewModels = "ClassLibrary.ViewModels"
};
ViewLocator.ConfigureTypeMappings(config);
ViewModelLocator.ConfigureTypeMappings(config);
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 ClassLibrary.Models.LogEntryModel.
- Я предполагаю, что мне нужно «сообщить» Caliburn для поиска моделей в моем проекте библиотеки классов (как)
- Следует ли мне ссылаться на Caliburn.Micro в моей библиотеке классов? (Поскольку это GUI вещь?)
- Где должны быть мои ViewModels, в ClassLibrary или в проекте GUI?
[edit] Я изменил Структура папок, моя виртуальная машина и модели теперь сгруппированы вместе, я обновил bootstrapper.cs:
var config = new TypeMappingConfiguration
{
DefaultSubNamespaceForViews = "WPFGUI.Views",
DefaultSubNamespaceForViewModels = "ClassLibrary.ViewModels"
};
ViewLocator.ConfigureTypeMappings(config);
ViewModelLocator.ConfigureTypeMappings(config);
ShellViewModel все еще функционирует; но LogEntryModel по-прежнему отображает:
Cannot find view for ClassLibrary.Models.LogEntryModel.
[edit 2] LogEntryModel:
public class LogEntryModel
{
//GUID
public Guid GUID { get; set; }
//The log message string
public string Message { get; set; }
//The module that created the logentry (see enums Module for options)
public int Module { get; set; }
//The urgency (used for coloring: 0 = black (normal), 1 = red (error), 2 = cyan (info)
public int Severity { get; set; }
//User that triggered the logentry
public string UserID { get; set; }
//The datetime of the logentry
public DateTime LogEntryDateTime { get; set; }
}
LogEntryViewModel:
public class LogEntryViewModel
{
//This is for testing purposes only (I'd expect "Hello World" everywhere
public String Message { get; set; } = "Hello World";
}
LogEntryView.xaml:
<UserControl x:Class="ServicesUI_WPF.Views.LogEntryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFGUI.Views"
DataContext="ClassLibrary.ViewModels.LogEntryViewModel"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="Red">
</Grid>
</UserControl>