简体   繁体   English

使用Caliburn Micro和Ninject设置依赖注入

[英]Setting up dependency injection with Caliburn Micro & Ninject

I'm trying to set up dependency injection in a new WPF project using the framework Caliburn Micro and Ninject. 我正在尝试使用Caliburn Micro和Ninject框架在新的WPF项目中设置依赖注入。 Unfortunately I'm not succeeding :( There are a few examples on the internet which implement a generic Bootstrap but for me the generic Bootstrap class is not available and since all these examples are at least 3 years old I guess they are deprecated... 不幸的是我没有成功:(在互联网上有一些例子实现了一个通用的Bootstrap,但对我来说,通用的Bootstrap类是不可用的,因为所有这些例子都至少3年了,我猜它们已经被弃用了......

What I've tried is the following: 我试过的是以下内容:

public class CbmBootstrapper : BootstrapperBase
{
    private IKernel kernel;

    protected override void Configure()
    {
        this.kernel = new StandardKernel();

        this.kernel.Bind<IAppViewModel>().To<AppViewModel>();
    }
}

And in the App.xaml 并在App.xaml中

<Application x:Class="CBMExample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:local="clr-namespace:CBMExample"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary>
            <local:CbmBootstrapper x:Key="bootstrapper" />
        </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

I am very new to WPF and Ninject. 我是WPF和Ninject的新手。 Can you tell me what I have to change, so that at startup of the application, the View (AppView) with the corresponding ViewModel (AppViewModel) gets loaded? 你能告诉我我要改变什么,以便在应用程序启动时,带有相应ViewModel(AppViewModel)的View(AppView)被加载吗?

Do you know of any up-to-date tutorial on using & setting up Ninject with Caliburn Micro? 您是否知道有关使用和设置Ninject与Caliburn Micro的最新教程?

You will need to override OnStartup as well to have your root view / viewmodel shown: 您还需要重写OnStartup以显示您的根视图/ viewmodel:

protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
    DisplayRootViewFor<IAppViewModel>();
}

This extra call replaced the previous, generic bootstrapper and allows you to choose the root view for your application at runtime. 此额外调用替换了先前的通用引导程序,并允许您在运行时为应用程序选择根视图。

You'll also need to override GetInstance to have Caliburn hook into Ninject: 您还需要覆盖GetInstance以使Caliburn挂钩到Ninject:

protected override object GetInstance(Type serviceType, string key)
{
    return container.Get(serviceType);
}

This is called by Caliburn.Micro whenever it needs to construct something, so it's your one-stop-shop for injecting Ninject ( other IoC containers are available! ) into the process. 无论什么时候需要构建一些东西,Caliburn.Micro都会调用它,所以这是一个注入Ninject( 其他IoC容器可用! )的一站式服务。

As for an up-to-date tutorial; 至于最新的教程; there aren't so many around since Caliburn.Micro went to version 2, however their official documentation is generally pretty useful. 自Caliburn以来没有那么多.Micro转到版本2,但是他们的官方文档通常非常有用。

EDIT: One more thing you have to do! 编辑:你还有一件事要做! Make sure that your bootstrapper constructor calls Initialize : 确保您的引导程序构造函数调用Initialize

public CbmBootstrapper ()
{           
    Initialize();
}

This will kick Caliburn.Micro into action... 这将使Caliburn.Micro付诸行动......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM