当前位置: 首页> 汽车> 行情 > Caliburn.Micro框架学习笔记——IOC配置

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

时间:2025/7/11 14:56:09来源:https://blog.csdn.net/weixin_42031602/article/details/139347753 浏览次数: 0次

如果我们想要自己写的程序更加模块化,这个时候就需要考虑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的步骤,仅供参考。

关键字:Caliburn.Micro框架学习笔记——IOC配置

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: