Windows phone 7 如何:Caliburn.Micro.Autofac和Windows Phone

Windows phone 7 如何:Caliburn.Micro.Autofac和Windows Phone,windows-phone-7,autofac,caliburn.micro,Windows Phone 7,Autofac,Caliburn.micro,有没有关于如何在Windows Phone上使用Caliburn.Micro.Autofac的示例、教程或其他内容 我用Caliburn.Micro创建了一个基本应用程序,运行良好。然后我决定使用Caliburn.Micro.Autofac,因此我从Caliburn.Micro.Autofac.AutofacBootstrapper派生了我的引导程序,并在引导程序Configure()方法中调用了base.Configure()。现在我运行了我得到的应用程序“找不到类型'AppBootstrap

有没有关于如何在Windows Phone上使用Caliburn.Micro.Autofac的示例、教程或其他内容

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


感谢您的帮助。

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

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
}
公共类AppBootstrapper:PhoneBootstrapper
{
私人集装箱;
受保护的void配置容器(ContainerBuilder生成器)
{
//在此处放置任何自定义绑定
}
#地区标准Autofac/Caliburn.Micro引导程序
受保护的覆盖无效配置()
{
//配置容器
var builder=new ContainerBuilder();
//注册电话服务
var caliburnAssembly=AssemblySource.Instance.Union(新[]{typeof(IStorageMechanism.Assembly}).ToArray();
//注册管理机制实现者
builder.RegisterAssemblyTypes(caliburnAssembly)
.Where(type=>typeof(IStorageMechanism).IsAssignableFrom(type)
&&!type.IsAbstract
&&!type.i接口)
.As()
.SingleInstance();
//注册IStorageHandler实现程序
builder.RegisterAssemblyTypes(caliburnAssembly)
.Where(type=>typeof(IStorageHandler).IsAssignableFrom(type)
&&!type.IsAbstract
&&!type.i接口)
.As()
.SingleInstance();
//必须调用这些服务的构造函数
//正确地连接到框架上。
var phoneService=新的PhoneApplicationServiceAdapter(根框架);
var navigationService=newframeadapter(RootFrame,false);
Register(c=>newautofachonecontainer(c)).SingleInstance();
builder.RegisterInstance(navigationService).SingleInstance();
RegisterInstance(phoneService).SingleInstance();
Register(c=>neweventaggregator()).SingleInstance();
注册(c=>newWindowManager()).SingleInstance();
Register(c=>newsystemcontroller()).SingleInstance();
Register(c=>newxnasoundeffectplayer()).SingleInstance();
builder.RegisterType().AsSelf().SingleInstance();
builder.RegisterType().AsSelf().SingleInstance();
//允许将派生类添加到容器中
配置容器(生成器);
//构建容器
container=builder.Build();
//启动服务
container.Resolve().Start();
container.Resolve().Start();
//为手机添加自定义约定
AddCustomConventions();
}
受保护的覆盖对象GetInstance(类型服务,字符串键)
{
对象实例;
if(string.IsNullOrEmpty(key))
{
if(container.TryResolve(服务,out实例))
返回实例;
}
其他的
{
if(container.TryResolveNamed(key、service、out实例))
返回实例;
}
抛出新异常(string.Format(“找不到协定{0}.”,键??service.Name)的任何实例);
}
受保护的重写IEnumerable GetAllInstances(类型服务)
{
返回container.Resolve(typeof(IEnumerable).MakeGenericType(service))作为IEnumerable;
}
受保护的覆盖空洞堆积(对象实例)
{
container.InjectProperties(实例);
}
私有静态void AddCustomConventions()
{
ConventionManager.AddElementConvention(Pivot.ItemsSourceProperty,“SelectedItem”,“SelectionChanged”).ApplyBinding=
(viewModelType、路径、属性、元素、约定)=>
{
国际单项体育联合会(会议经理)
.GetElementConvention(typeof(ItemsControl))
.ApplyBinding(viewModelType、路径、属性、元素、约定))
{
会议经理
.ConfigureSelectedItem(元素,透视。SelectedItemProperty,viewModelType,路径);
会议经理
.ApplyHeaderTemplate(元素,Pivot.HeaderTemplateProperty,viewModelType);
返回true;
}
返回false;
};
ConventionManager.AddElementConvention(Panorama.ItemsSourceProperty,“SelectedItem”,“SelectionChanged”).ApplyBinding=
(viewModelType、路径、属性、元素、约定)=>
{
国际单项体育联合会(会议经理)
.GetElementConvention(typeof(ItemsControl))
.ApplyBinding(viewModelType、路径、属性、元素、约定))
{
会议经理
.ConfigureSelectedItem(元素、全景、SelectedItemProperty、viewModelType、路径);
会议经理
.ApplyHeaderTemplate(元素,Panorama.HeaderTemplateProperty,viewModelType);
返回true;
}
返回false;
};
}
#端区
}
编辑我创建了Caliburn.Micro.Autofac的分支,并在GitHub上解决了这个问题。希望pull请求能够被接受,这将成为主存储库的一部分


现在,您可以从这里访问引导程序和AutofacPhoneContainer-

这是我为WP7项目编写的引导程序。它基于Caliburn.Micro