首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何: Caliburn.Micro.Autofac和Windows

如何: Caliburn.Micro.Autofac和Windows
EN

Stack Overflow用户
提问于 2011-12-06 16:07:11
回答 2查看 1.3K关注 0票数 1

是否有说明如何在Windows中使用Caliburn.Micro.Autofac的示例、教程或任何内容?

我只使用Caliburn.Micro创建了一个基本应用程序,运行良好。然后我决定使用Caliburn.Micro.Autofac,因此我从Caliburn.Micro.Autofac.AutofacBootstrapper派生了Bootstrapper,并在Bootstrapper Configure()方法中调用了base.Configure()。现在,我运行了一个应用程序,我得到了“找不到'AppBootstrapper‘类型’”。异常。

感谢你的帮助。

EN

回答 2

Stack Overflow用户

发布于 2011-12-19 23:35:46

这是我为WP7项目编写的引导程序。它基于Caliburn.Micro.Autofac.AutofacBootstrapper,但修复了一些bug。

代码语言:javascript
复制
public class AppBootstrapper : PhoneBootstrapper
{
    private IContainer container;

    protected void ConfigureContainer(ContainerBuilder builder)
    {
        // put any custom bindings here
    }

    #region Standard Autofac/Caliburn.Micro Bootstrapper

    protected override void Configure()
    {
        //  configure container
        var builder = new ContainerBuilder();

        //  register phone services
        var caliburnAssembly = AssemblySource.Instance.Union(new[] { typeof(IStorageMechanism).Assembly }).ToArray();
        //  register IStorageMechanism implementors
        builder.RegisterAssemblyTypes(caliburnAssembly)
          .Where(type => typeof(IStorageMechanism).IsAssignableFrom(type)
                         && !type.IsAbstract
                         && !type.IsInterface)
          .As<IStorageMechanism>()
          .SingleInstance();

        //  register IStorageHandler implementors
        builder.RegisterAssemblyTypes(caliburnAssembly)
          .Where(type => typeof(IStorageHandler).IsAssignableFrom(type)
                         && !type.IsAbstract
                         && !type.IsInterface)
          .As<IStorageHandler>()
          .SingleInstance();

        // The constructor of these services must be called
        // to attach to the framework properly.
        var phoneService = new PhoneApplicationServiceAdapter(RootFrame);
        var navigationService = new FrameAdapter(RootFrame, false);

        builder.Register<IPhoneContainer>(c => new AutofacPhoneContainer(c)).SingleInstance();
        builder.RegisterInstance<INavigationService>(navigationService).SingleInstance();
        builder.RegisterInstance<IPhoneService>(phoneService).SingleInstance();
        builder.Register<IEventAggregator>(c => new EventAggregator()).SingleInstance();
        builder.Register<IWindowManager>(c => new WindowManager()).SingleInstance();
        builder.Register<IVibrateController>(c => new SystemVibrateController()).SingleInstance();
        builder.Register<ISoundEffectPlayer>(c => new XnaSoundEffectPlayer()).SingleInstance();
        builder.RegisterType<StorageCoordinator>().AsSelf().SingleInstance();
        builder.RegisterType<TaskController>().AsSelf().SingleInstance();

        //  allow derived classes to add to the container
        ConfigureContainer(builder);

        //  build the container
        container = builder.Build();

        //  start services
        container.Resolve<StorageCoordinator>().Start();
        container.Resolve<TaskController>().Start();

        //  add custom conventions for the phone
        AddCustomConventions();
    }

    protected override object GetInstance(Type service, string key)
    {
        object instance;
        if (string.IsNullOrEmpty(key))
        {
            if (container.TryResolve(service, out instance))
                return instance;
        }
        else
        {
            if (container.TryResolveNamed(key, service, out instance))
                return instance;
        }
        throw new Exception(string.Format("Could not locate any instances of contract {0}.", key ?? service.Name));
    }

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

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

    private static void AddCustomConventions()
    {
        ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
            (viewModelType, path, property, element, convention) =>
            {
                if (ConventionManager
                    .GetElementConvention(typeof(ItemsControl))
                    .ApplyBinding(viewModelType, path, property, element, convention))
                {
                    ConventionManager
                        .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
                    ConventionManager
                        .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, viewModelType);
                    return true;
                }

                return false;
            };

        ConventionManager.AddElementConvention<Panorama>(Panorama.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
            (viewModelType, path, property, element, convention) =>
            {
                if (ConventionManager
                    .GetElementConvention(typeof(ItemsControl))
                    .ApplyBinding(viewModelType, path, property, element, convention))
                {
                    ConventionManager
                        .ConfigureSelectedItem(element, Panorama.SelectedItemProperty, viewModelType, path);
                    ConventionManager
                        .ApplyHeaderTemplate(element, Panorama.HeaderTemplateProperty, viewModelType);
                    return true;
                }

                return false;
            };
    }

    #endregion
}

编辑我创建了一个Caliburn.Micro.Autofac分叉,并在GitHub上解决了这个问题。希望拉请求将被接受,这将成为主要存储库的一部分。

现在,您可以从这里访问引导程序和AutofacPhoneContainer - https://github.com/distantcam/Caliburn.Micro.Autofac/tree/master/src/Caliburn.Micro.Autofac-WP7

票数 2
EN

Stack Overflow用户

发布于 2012-06-03 23:21:57

我已经为Windows实现了一个正确的Caliburn.Micro.Autofac版本(在我看来)。您可以从我的博客下载并测试项目。博客文章是用俄语写的,但是你可以在文章的顶部找到指向ZIP文件的链接。代码太大了,不能在这里发布,所以请从博客中获取。我已将此发送给David (Caliburn.Micro.Autofac的作者)。希望他能很快把它整合到他的代码库中。

更新

固定的是:

  1. 在容器中注册之前,必须实例化实现IPhoneService和INavigationService服务的组件。
  2. 实现了实现IPhoneContainer的组件。没有它,您就不能在Caliburn.Micro中使用Autofac。
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8403007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档