Caliburn.Micro框架学习笔记——IOC配置

如果我们想要自己写的程序更加模块化,这个时候就需要考虑IOC容器注入的思维。在使用Caliburn.Micro时该如何配置?其基本步骤如下——

  1. 安装 Caliburn.Micro 包。
  2. 创建并配置 AppBootstrapper 类。
  3. 配置 App.xaml 以使用 AppBootstrapper 启动应用程序。
  4. 创建视图和视图模型。
  5. 注册服务并在视图模型中使用依赖注入。

通过这些步骤,你可以充分利用 Caliburn.Micro 提供的 IoC 和 DI 功能,简化应用程序的开发。

 具体过程

配置 Bootstrapper

Caliburn.Micro 使用一个 Bootstrapper 类来配置和启动应用程序。在这个类中,你可以设置 IoC 容器、注册服务和视图模型等。

创建一个继承自 BootstrapperBase 的类

public class AppBootstrapper : BootstrapperBase
    {
        private SimpleContainer _container;

        public AppBootstrapper()
        {
            Initialize();
        }

        protected override void Configure()
        {
            _container = new SimpleContainer();

            // 注册服务和视图模型
            _container.Singleton<IWindowManager, WindowManager>();
            _container.Singleton<IEventAggregator, EventAggregator>();
            _container.PerRequest<ShellViewModel>();
        }

        protected override object GetInstance(Type service, string key)
        {
            var instance = _container.GetInstance(service, key);
            if (instance != null)
            {
                return instance;
            }

            throw new InvalidOperationException($"Could not locate any instances of contract {service.Name}.");
        }

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

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

        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor<ShellViewModel>();
        }
    }

 其中,这几个方法的作用是——

  • Configure 方法:用于配置 IoC 容器并注册服务和视图模型。
  • GetInstance 方法:用于从容器中解析实例。
  • GetAllInstances 方法:用于获取所有实例。
  • BuildUp 方法:用于构建实例。
  • OnStartup 方法:应用程序启动时显示主视图。

 配置 App.xaml

App.xaml 文件中,设置 AppBootstrapper 为启动类:

<Application x:Class="YourNamespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="AppBootstrapper.xaml">
    <Application.Resources>
        
    </Application.Resources>
</Application>

创建相应的View和ViewModel

View

<Window x:Class="YourNamespace.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ShellView" Height="350" Width="525">
    <Grid>
        <TextBlock Text="Hello, Caliburn.Micro!" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</Window>

ViewModel

using Caliburn.Micro;

namespace YourNamespace
{
    public class ShellViewModel : Screen
    {
        // 视图模型逻辑
    }
}

服务注入和使用

假设你有一个服务 IMyService 和它的实现 MyService,可以在 Configure 方法中注册它们,并在视图模型中使用它们。其中MyService继承于IMyService,然后在 AppBootstrapper 中注册服务:

protected override void Configure()
{
    _container = new SimpleContainer();

    _container.Singleton<IWindowManager, WindowManager>();
    _container.Singleton<IEventAggregator, EventAggregator>();
    _container.Singleton<IMyService, MyService>(); // 注册服务
    _container.PerRequest<ShellViewModel>();
}

 _container.Singleton<IMyService, MyService>(); // 注册服务

此时该服务被注册为单例的形式。

当我们想要使用时,在ViewModel中有

1)通过构造函数将这个IService类进行引入

2)使用

public class ShellViewModel : Screen
{
    private readonly IMyService _myService;

    public ShellViewModel(IMyService myService)
    {
        _myService = myService;
    }

    public void ExecuteService()
    {
        _myService.DoSomething();//这个就是Iservice中的方法
    }
}

 以上就是通过Caliburn.Micro的IOC的步骤,仅供参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matrix Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值