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

embedded/2024/10/18 7:55:49/

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


http://www.ppmy.cn/embedded/44621.html

相关文章

【机器学习】Adaboost: 强化弱学习器的自适应提升方法

&#x1f308;个人主页: 鑫宝Code &#x1f525;热门专栏: 闲话杂谈&#xff5c; 炫酷HTML | JavaScript基础 ​&#x1f4ab;个人格言: "如无必要&#xff0c;勿增实体" 文章目录 Adaboost: 强化弱学习器的自适应提升方法引言Adaboost基础概念弱学习器与强学习…

如何防止重复提交请求?

下面说的防重操作&#xff0c;如支付功能订单提交业务、表单提交、手机验证码功能。 订单提交为什么需要防重呢&#xff1f;想像一下你在商城购物&#xff0c;你选中商品点击提交订单&#xff0c;如果这时网络延迟没有返回成功提示&#xff0c;你又多点了几次。每点一次都会发送…

css-垂直居中的几种写法

图示 1、使用line-height属性&#xff08;当div有固定高度时&#xff09; 2、使用flexbox布局

centos7安装MySQL

文章目录 centos7 aarch64安装MySQL5.7.27errorERROR 1045 (28000): Access denied for user rootlocalhost (using password: NO) yum安装mysql8运行初始化 errorDBMS: MySQL (版本 8.4.0) 区分大小写: 普通形式mixed&#xff0c;分隔形式exact NotBefore: Wed May 29 13:09:1…

C 语言实例 - 表格形式输出数据

将 1~100 的数据以 10x10 矩阵格式输出。 #include <stdio.h>int main() {int i, j, count;for(i 1; i < 10; i) {for(j i; j <100; j 10 )printf(" %3d", j);printf("\n");}return 0; }运行结果&#xff1a; 1 11 21 31 41 51 61 …

QT常用快捷键

Qt creator 最常用的13个快捷键 alt enter // 自动创建类的定义 F1 // 查看帮助&#xff0c;文档 F2 // 快速到变量声明 Shift F2 // 函数的声明和定义之间快速切换 F4 // 在 cpp 和 h 文件切换 Ctrl M 创建书签&#xff0c; Ctrl . 切换书签 Alt M打开书签栏。 Ctrl…

C# 类的深入指南

C#中的类是面向对象编程的核心概念之一。本篇博客将详细介绍C#类中的字段、常量、方法、构造器、对象初始化器、this引用、属性、索引器、静态构造器、静态类、终结器以及类特性和修饰符。 类的字段 字段是类或结构体中存储数据的成员。 public class Person {public string…

从零开始利用MATLAB进行FPGA设计(七)用ADC采集信号教程2

黑金的教程做的实在太拉闸了&#xff0c;于是自己摸索信号采集模块的使用方法。 ADC模块&#xff1a;AN9238 FPGA开发板&#xff1a;AX7020&#xff1b;Xilinx 公司的 Zynq7000 系列的芯片XC7Z020-2CLG400I&#xff0c;400引脚 FBGA 封装。 往期回顾&#xff1a; 从零开始利…