Caliburn.Micro - IoC未初始化

时间:2014-12-02 10:26:46

标签: ioc-container caliburn.micro

在我的Windows Phone 8.1应用程序(本机,非Silverlight)中,我使用的是Caliburn.Micro。 在我的应用程序的某些部分,我必须使用服务定位器而不是构造函数注入,所以我称之为:

var service = IoC.Get<ITestClass>();

但是当我打电话给它时,我得到了:

An exception of type 'System.InvalidOperationException' occurred in Caliburn.Micro.DLL but was not handled in user code

Additional information: IoC is not initialized.

我的初始化类是:

public sealed partial class App
{
    private WinRTContainer container;

    public App()
    {
        this.InitializeComponent();
    } 

    protected override void Configure()
    {
        container = new WinRTContainer();
        container.RegisterWinRTServices();

        Register(container);

        IoC.GetInstance = this.GetInstance;
        IoC.GetAllInstances = this.GetAllInstances;
        IoC.BuildUp = this.BuildUp;
    }

    private void Register(WinRTContainer container)
    {
        // Automatically register all view models
        foreach(var type in typeof(ViewModelBase).GetTypeInfo().Assembly.DefinedTypes.Where(type => type.IsSubclassOf(typeof(ViewModelBase))))
        {
            if (type.IsClass && !type.IsAbstract)
            {
                container.RegisterPerRequest(type.ImplementedInterfaces.Count() == 1 ? type.ImplementedInterfaces.First() : type.AsType(), null, type.AsType());
            }
        }

        container.PerRequest<ITestClass, TestClass>();
        container.RegisterInstance(typeof(ResourceLoader), "ResourceLoader", new ResourceLoader());
    } 

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        #if DEBUG
        if (System.Diagnostics.Debugger.IsAttached)
        {
            this.DebugSettings.EnableFrameRateCounter = true;
        }
        #endif
        DisplayRootView<MainPageView>();
    } 

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

    protected override void PrepareViewFirst(Frame rootFrame)
    {
        container.RegisterNavigationService(rootFrame);
    }

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

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

修改 问题是我称之为 IoC.Get&lt; ITest&gt;() metod在IBackgroundTask的实现中。在另一个线程上,属性 IoC.GetInstance 再次为null。为什么呢?

由于

0 个答案:

没有答案