1、 容器的支持情况。
由于Prism.DryIoc支持的平台更多,可以退从NuGet中直接引入,所以我们选择使用Prism.DryIoc做依赖注入。需要注意的是虽然 DryIoc 和 Unity Container 在 NuGet.org 上可用,但它们仍受 Prism 许可证的约束。您应该拥有有效的 Prism 许可证。
2、服务的注册。注册可以注册临时服务和注册单例服务。
2.1、新建ServiceBLL文件夹,新建接口IPrintService.cs(我搞饭了,这个接口应该创建在ServiceCore文件夹下),代码如下
cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp1.ServiceBLL
{
public interface IPrintService
{
public void Print();
}
}
2.2、新建ServiceCore文件夹,新建接口PrintService.cs(我搞饭了,这个接口应该创建在ServiceBLL文件夹下),代码如下
cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp1.ServiceBLL
{
public class PrintService:IPrintService
{
public string PrintContent { get; set; } = "未设置内容";
public void Print()
{
Debug.WriteLine(PrintContent);
}
}
}
2.3、新建ViewModels文件夹,创建MainWindowViewModel.cs代码如下。
cs
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp1.ServiceBLL;
namespace WpfApp1.ViewModels
{
public class MainWindowViewModel
{
private readonly IContainerProvider _containerProvider;
public MainWindowViewModel(IContainerProvider containerProvider)
{
_containerProvider = containerProvider;
PrintService _PrintService = _containerProvider.Resolve<PrintService>();
_PrintService.PrintContent = "已设置内容";
_PrintService.Print();
}
private DelegateCommand _PrintCommand;
public DelegateCommand PrintCommand
{
get
{
return _PrintCommand ?? (_PrintCommand = new DelegateCommand(Print));
}
}
private void Print()
{
_containerProvider.Resolve<PrintService>().Print();
}
}
}
2.4、创建Views文件夹,将MainWindow.xaml文件移动到Views文件夹中,代码如下。
XML
<Window x:Class="WpfApp1.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<TextBox x:Name="tbText" Width="200" Height="30" HorizontalAlignment="Center" Text="设置内容"/>
<Button Width="200" Height="30" Content="点击" Command="{Binding PrintCommand}"/>
</StackPanel>
</Window>
2.5、App.xaml.cs文件代码如下。
cs
using Prism.Ioc;
using System.Configuration;
using System.Data;
using System.Printing;
using System.Windows;
using WpfApp1.ServiceBLL;
using WpfApp1.Views;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//containerRegistry.Register<PrintService>();
//containerRegistry.Register<IPrintService,PrintService>();
containerRegistry.RegisterInstance<PrintService>(new PrintService());
}
}
}
注册临时服务可以这样使用,containerRegistry.Register<PrintService>();和
containerRegistry.Register<IPrintService,PrintService>();
注册单例服务可以这样使用,containerRegistry.RegisterInstance<PrintService>(new PrintService());
2.6、检查服务是否已注册,可以通过以下方检擦,代码如下。
cs
if (_containerProvider.IsRegistered<PrintService>())
{
_containerProvider.Resolve<PrintService>().Print();
}
2.6、延迟加载服务Lazy<IFoo>方式注册服务,MainWindowViewModel.cs代码如下。
cs
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp1.ServiceBLL;
namespace WpfApp1.ViewModels
{
public class MainWindowViewModel
{
private readonly IContainerProvider _containerProvider;
private readonly PrintService _printService;
public MainWindowViewModel(IContainerProvider containerProvider, Lazy<PrintService> printService)
{
_containerProvider = containerProvider;
_printService = printService.Value;
_printService.PrintContent = "已设置内容";
}
private DelegateCommand _PrintCommand;
public DelegateCommand PrintCommand
{
get
{
return _PrintCommand ?? (_PrintCommand = new DelegateCommand(Print));
}
}
private void Print()
{
_printService.Print();
}
}
}
Func<PrintService> printService 方式注册服务,MainWindowViewModel.cs代码如下。
cs
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp1.ServiceBLL;
namespace WpfApp1.ViewModels
{
public class MainWindowViewModel
{
private readonly IContainerProvider _containerProvider;
private readonly PrintService _printService;
public MainWindowViewModel(IContainerProvider containerProvider, Func<PrintService> printService)
{
_containerProvider = containerProvider;
_printService = printService.Invoke();
_printService.PrintContent = "已设置内容";
}
private DelegateCommand _PrintCommand;
public DelegateCommand PrintCommand
{
get
{
return _PrintCommand ?? (_PrintCommand = new DelegateCommand(Print));
}
}
private void Print()
{
_printService.Print();
}
}
}