操作方法:Caliburn.Micro.Autofac 和 Windows Phone

发布于 2024-12-19 17:04:41 字数 370 浏览 6 评论 0原文

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

我仅使用 Caliburn.Micro 创建了一个基本应用程序,并且运行良好。然后我决定使用 Caliburn.Micro.Autofac,因此我从 Caliburn.Micro.Autofac.AutofacBootstrapper 派生了我的 Bootstrapper,并在 Bootstrapper 中调用 base.Configure() >Configure() 方法。现在,当我运行该应用程序时,我得到“未找到类型‘AppBootstrapper’”。例外。

感谢任何帮助。

Is there and example, tutorial or anything that shows how to use Caliburn.Micro.Autofac with Windows Phone?

I created a basic application with Caliburn.Micro only, and that runs fine. Then I decided to use Caliburn.Micro.Autofac, so I derived my Bootstrapper from Caliburn.Micro.Autofac.AutofacBootstrapper and called base.Configure() inside the Bootstrapper Configure() method. Now wen I ran the application I get "The type 'AppBootstrapper' was not found." exception.

Appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十雾 2024-12-26 17:04:41

这是我为 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
}

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

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

This is the bootstrapper I wrote for a WP7 project. It's based on Caliburn.Micro.Autofac.AutofacBootstrapper but fixes some bugs.

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 I have created a fork of Caliburn.Micro.Autofac and fixed the issue on GitHub. Hopefully the pull request will be accepted and this will become part of the main repository.

For now, you can access the bootstrapper, and AutofacPhoneContainer from here - https://github.com/distantcam/Caliburn.Micro.Autofac/tree/master/src/Caliburn.Micro.Autofac-WP7

ま柒月 2024-12-26 17:04:41

我已经为 Windows Phone 实现了 Caliburn.Micro.Autofac 的正确版本(在我看来)。您可以从我的博客下载它并测试项目。该博文是俄语的,但您可以在博文顶部找到 ZIP 文件的链接。代码太大,无法在此处发布,因此请从博客中获取。我已将其发送给 David Buksbaum(Caliburn.Micro.Autofac 的作者)。希望他尽快将其合并到他的代码库中。

更新

修复内容:

  1. 实现 IPhoneService 和 INavigationService 服务的组件必须在注册到容器中之前实例化。
  2. 实现了IPhoneContainer的组件。没有它,您将无法在 Caliburn.Micro 中使用 Autofac。

I have implemented a proper version (in my opinion) of Caliburn.Micro.Autofac for Windows Phone. You can download it and test project from my blog. The blog post is in Russian but you'll find the link to ZIP file in the top of the post. The code is too big to post here, so please take from the blog. I've send this to David Buksbaum (the author of Caliburn.Micro.Autofac). Hope he will incorporate it into his code base soon.

UPDATE

What is fixed:

  1. Components realizing IPhoneService and INavigationService services must be instantiated before registering in container.
  2. Realized component implementing IPhoneContainer. Without it you can't use Autofac in Caliburn.Micro.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文