如果我们想要自己写的程序更加模块化,这个时候就需要考虑IOC容器注入的思维。在使用Caliburn.Micro时该如何配置?其基本步骤如下——
- 安装 Caliburn.Micro 包。
- 创建并配置
AppBootstrapper
类。- 配置
App.xaml
以使用AppBootstrapper
启动应用程序。- 创建视图和视图模型。
- 注册服务并在视图模型中使用依赖注入。
通过这些步骤,你可以充分利用 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的步骤,仅供参考。