我正在尝试使用 caliburn 微框架做一个基本的 MVVM WPF 应用程序。我的想法是做下一个结构:
登录对话框 --> 带有 3 个按钮的主窗口
--> 1. 带有 tabcontrol 的
屏幕 --> 2. 简单屏幕
--> 3. 简单屏幕
我已经阅读了很多关于 caliburn 的文章并下载了几个示例,但我仍然不确定要遵循的步骤。我知道我必须创建一个 WindowManager 来管理对话框、弹出窗口等……以及一个管理活动屏幕的指挥器。我实现了正确打开主窗口菜单:
引导程序:
public class Bootstrapper : Bootstrapper<MainWindowViewModel>
{
CompositionContainer _container;
public Bootstrapper()
{
}
protected override void Configure()
{
var catalog = new AggregateCatalog(
AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());
_container = new CompositionContainer(catalog);
var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(_container);
_container.Compose(batch);
}
protected override void BuildUp(object instance)
{
_container.SatisfyImportsOnce(instance);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.GetExportedValues<object>(AttributedModelServices.GetContractName(service));
}
protected override object GetInstance(Type service, string key)
{
string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : key;
var exports = _container.GetExportedValues<object>(contract);
if (exports.Count() > 0)
return exports.First();
throw new Exception(string.Format("Could not locate any instance of contract {0}.", contract));
}
}
主窗口视图模型:
namespace MVVM_Caliburn_Sample1
{
[Export(typeof(MainWindowViewModel))]
public class MainWindowViewModel : Conductor<IScreen>.Collection.OneActive
{
readonly IWindowManager windowManager;
[ImportingConstructor]
public MainWindowViewModel(IWindowManager windowManager)
{
DisplayName = "Composition App";
this.windowManager = windowManager;
}
public void OpenWin() {
dynamic settings = new ExpandoObject();
settings.Placement = PlacementMode.Center;
settings.PlacementTarget = GetView(null);
windowManager.ShowDialog(new TabsViewModel(this.windowManager),null,settings);
}
public void OpenWin2() {
var result = windowManager.ShowDialog(new TwoWindowViewModel(this.windowManager));
}
public void OpenWin3() {
//TO-DO
}
}
}
带有选项卡控件的屏幕:
namespace MVVM_Caliburn_Sample1
{
[Export(typeof(IScreen))]
public class TabsViewModel : Conductor<IScreen>.Collection.OneActive
{
readonly IWindowManager windowManager;
[ImportingConstructor]
public TabsViewModel(IWindowManager windowManager)
{
DisplayName = "Tabs View";
this.windowManager = windowManager;
Items.Add(new TabOneViewModel());
Items.Add(new TabTwoViewModel());
}
}
}
TabOneViewModel:
namespace MVVM_Caliburn_Sample1
{
[Export(typeof(IScreen))]
public class TabOneViewModel : Screen{
[ImportingConstructor]
public TabOneViewModel()
{
DisplayName = "View #1";
}
}
}
我做得对(打开窗户,屏幕......)?MainWindowViewModel 必须有自己的导体,而 TabsViewModel 必须有另一个用于选项卡的导体?我必须像我一样发送窗口管理器吗?总的来说,您如何看待这个样本?
对不起,我知道我应该犯错,但我正在学习。
谢谢您的反馈!