WPF Prism Services依赖注入
一.安装Prism.Dryloc,当前最新版为7.2*
二.App.xaml.cs中RegisterTypes中的依赖注入写法与ASP.netcore相似
App.xaml
// App.xaml
<prism:PrismApplication x:Class="Prism.Wpf.NetCore.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Prism.Wpf.NetCore"
xmlns:prism="http://prismlibrary.com/" >
<Application.Resources>
</Application.Resources>
</prism:PrismApplication>
App.xaml.cs
// App.xaml.cs
using Prism.Ioc;
using Prism.Wpf.NetCore.Views;
using System.Windows;
using Prism.Modularity;
using Prism.Wpf.NetCore.Services;
namespace Prism.Wpf.NetCore
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<IDataService, DataService>();
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
base.ConfigureModuleCatalog(moduleCatalog);
moduleCatalog.AddModule<MainModule>();
}
}
}
三.可在ViewModel中构造函数注入
注入
UserInfoViewModel.cs
调用