Caliburn Micro WPF 窗口管理

发布于 2024-11-29 10:52:24 字数 2387 浏览 1 评论 0原文

我想使用 caliburn.micro 启动一个 WPF 应用程序,这样我就可以尽可能地使用 TDD,我之前在 WP7 中使用过 caliburn.micro,但 WPF 似乎是另一艘船,并且文档不完整与 WP7 一样。

我已经用我的 Bootstrapper 设置了该项目

public class ApplicationBootstrapper : Bootstrapper
{
    private SimpleContainer _container;
    private WindowManager _windowManager;

    protected override void Configure()
    {
        _container = new SimpleContainer();
        _windowManager = new WindowManager();

        _container.RegisterSingleton(typeof(MainViewModel), "MainViewModel", typeof(MainViewModel));
        _container.RegisterSingleton(typeof(DataViewModel), "DataViewModel", typeof(DataViewModel));

    }

    protected override object GetInstance(Type service, string key)
    {
        return _container.GetInstance(service, key);
    }

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return _container.GetAllInstances(service);
    }

    protected override void BuildUp(object instance)
    {
        _container.BuildUp(instance);
    }

    protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
    {
        _windowManager.ShowWindow(_container.GetInstance(typeof(MainViewModel), "MainViewModel"));
    }
}

,这很好地加载了 MainView,这让我认为我赢了,但我当时想继续前进并进行另一个视图/视图模型设置,但我一生都无法获得 WindowManager传递到我的 ViewModel 以加载更多视图(就像我在 WP7 中使用 NavigationService 所做的那样)

这是我的 MainViewModel 代码

public class MainViewModel : BaseViewModel
{
    private readonly DataViewModel _dataViewModel;
    private readonly IWindowManager _windowManager;


    public MainViewModel(IWindowManager windowManager, DataViewModel dataViewModel)
    {

        _dataViewModel = dataViewModel;
        _windowManager = windowManager;
    }

    public string Title { get { return ApplicationTitle; } }

    public void BtnNew()
    {
        System.Diagnostics.Debug.WriteLine(_windowManager == null);

    }

    public void BtnLoad()
    {
        MessageBox.Show("Sorry, not yet implemented");
    }
}

但是 Window Manger 和 DataViewModel 始终为空,当我在互联网上搜索解决方案时我发现它确实是我想要利用的 WindowManager,以及有关 MEFBootStrapper 的东西?但我本以为我在 WP7 上使用的框架约定会比看上去的效果好很多。

Simple Container 只是 caliburn.micro 在其网站上提供的配方 ,以及我在 WP7 应用程序中使用的那个

I'm wanting to start a WPF application using caliburn.micro so I can use TDD as much as possible, I've used caliburn.micro before with WP7 but WPF seems to be a different ship, and the documentation isn't as complete as that for WP7.

I've set up the project with my Bootstrapper

public class ApplicationBootstrapper : Bootstrapper
{
    private SimpleContainer _container;
    private WindowManager _windowManager;

    protected override void Configure()
    {
        _container = new SimpleContainer();
        _windowManager = new WindowManager();

        _container.RegisterSingleton(typeof(MainViewModel), "MainViewModel", typeof(MainViewModel));
        _container.RegisterSingleton(typeof(DataViewModel), "DataViewModel", typeof(DataViewModel));

    }

    protected override object GetInstance(Type service, string key)
    {
        return _container.GetInstance(service, key);
    }

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return _container.GetAllInstances(service);
    }

    protected override void BuildUp(object instance)
    {
        _container.BuildUp(instance);
    }

    protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
    {
        _windowManager.ShowWindow(_container.GetInstance(typeof(MainViewModel), "MainViewModel"));
    }
}

and this loads the MainView fine, which made me think I had won but I was then wanting to move on and have another view/viewmodel setup but for the life of me I cannot get the WindowManager to be passed through to my ViewModel in order to load more views (As I did with the NavigationService in WP7)

Here is my MainViewModel code

public class MainViewModel : BaseViewModel
{
    private readonly DataViewModel _dataViewModel;
    private readonly IWindowManager _windowManager;


    public MainViewModel(IWindowManager windowManager, DataViewModel dataViewModel)
    {

        _dataViewModel = dataViewModel;
        _windowManager = windowManager;
    }

    public string Title { get { return ApplicationTitle; } }

    public void BtnNew()
    {
        System.Diagnostics.Debug.WriteLine(_windowManager == null);

    }

    public void BtnLoad()
    {
        MessageBox.Show("Sorry, not yet implemented");
    }
}

But the Window Manger and the DataViewModel is always null, when I searched the internet for a solution I found that it was indeed the WindowManager that I wanted to utilize, and something about the MEFBootStrapper? but I would have thought that the framework conventions that I used on WP7 would have transferred a tonne better than it seems to be.

The Simple Container is just the recipe provided by caliburn.micro on their site, and the one I used in WP7 applications

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2024-12-06 10:52:24

您没有向 IOC 容器注册 WindowManager。
看一下 Caliburn.Micro.HelloWindowManager 示例项目。

你可以在这里下载:
http://caliburnmicro.codeplex.com/releases/view/70940

you didnt register the WindowManager with the IOC container.
take a look at Caliburn.Micro.HelloWindowManager sample project.

you can download it here:
http://caliburnmicro.codeplex.com/releases/view/70940

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文