如何使用:Caliburn.Micro.Autofac和Windows Phone



是否有一个例子,教程或任何东西显示如何使用Caliburn.Micro.Autofac与Windows Phone?

我用Caliburn创建了一个基本的应用程序。只有微型,运行良好。然后我决定用Caliburn.Micro。所以我从Caliburn.Micro.Autofac.AutofacBootstrapper派生了我的Bootstrapper,并在Bootstrapper Configure()方法中调用base.Configure()。现在,当我运行应用程序时,我得到"类型'AppBootstrapper' was not found."异常。

感谢您的帮助。

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

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
}

EDIT我已经创建了Caliburn.Micro.Autofac的分支并在GitHub上修复了这个问题。希望拉取请求将被接受,这将成为主存储库的一部分。

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

我已经为Windows Phone实现了一个合适的版本(在我看来)caliburn . micro . autoface。你可以从我的博客下载和测试项目。这篇博文是俄语的,但你会在文章的顶部找到ZIP文件的链接。代码太大了,不能在这里发布,所以请从博客中获取。我把这个发给了David Buksbaum (Caliburn.Micro.Autofac的作者)。希望他能尽快把它合并到他的代码库中。

什么是固定:

  1. 实现iphone服务和INavigationService服务的组件必须在注册到容器之前被实例化。
  2. 实现IPhoneContainer的已实现组件。如果没有它,你就不能在Caliburn.Micro中使用autofacc。

相关内容

  • 没有找到相关文章

最新更新