模块 DLL
ModuleA 和 ModuleB 都要安装 Prism.Unity
引用方式1 项目引用
直接 在引用中添加项目引用
App.xaml.cs 添加模块 ConfigureModuleCatalog
using ModuleA;
using ModuleB;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;
using PrismWpfApp.ViewModels;
using PrismWpfApp.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;namespace PrismWpfApp
{/// <summary>/// App.xaml 的交互逻辑/// </summary>public partial class App : PrismApplication{/// <summary>/// 提供主窗口的对象/// </summary>/// <returns></returns>/// <exception cref="NotImplementedException"></exception>protected override Window CreateShell(){//打开第一个窗口 ModuleWindow//return Container.Resolve<MainWindow>();return Container.Resolve<ModuleWindow>();//return new MainWindow();}/// <summary>/// 业务中所有需要注入的对象,在这个方法里注册/// </summary>/// <param name="containerRegistry"></param>/// <exception cref="NotImplementedException"></exception>protected override void RegisterTypes(IContainerRegistry containerRegistry){//注册 导航 RegisterForNavigationcontainerRegistry.RegisterForNavigation<UserControlA>();containerRegistry.RegisterForNavigation<UserControlB>();containerRegistry.RegisterForNavigation<UserControlC>();}/// <summary>/// 添加模块/// </summary>/// <param name="moduleCatalog"></param>protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog){moduleCatalog.AddModule<ModuleAProfile>();moduleCatalog.AddModule<ModuleBProfile>();base.ConfigureModuleCatalog(moduleCatalog);}//protected override void OnStartup(StartupEventArgs e)//{// base.OnStartup(e);// if (new LoginWindow().ShowDialog() == true)// {// new MainWindow().ShowDialog();// }// Application.Current.Shutdown();//}}
}
引用方式2 通过Modules目录动态注册
App.xaml.cs 创建模块 CreateModuleCatalog()
using Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;
using PrismWpfApp.ViewModels;
using PrismWpfApp.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;namespace PrismWpfApp
{/// <summary>/// App.xaml 的交互逻辑/// </summary>public partial class App : PrismApplication{/// <summary>/// 提供主窗口的对象/// </summary>/// <returns></returns>/// <exception cref="NotImplementedException"></exception>protected override Window CreateShell(){//打开第一个窗口 ModuleWindow//return Container.Resolve<MainWindow>();return Container.Resolve<ModuleWindow>();//return new MainWindow();}/// <summary>/// 业务中所有需要注入的对象,在这个方法里注册/// </summary>/// <param name="containerRegistry"></param>/// <exception cref="NotImplementedException"></exception>protected override void RegisterTypes(IContainerRegistry containerRegistry){//注册 导航 RegisterForNavigationcontainerRegistry.RegisterForNavigation<UserControlA>();containerRegistry.RegisterForNavigation<UserControlB>();containerRegistry.RegisterForNavigation<UserControlC>();}/// <summary>/// 添加模块(代码引用)/// </summary>/// <param name="moduleCatalog"></param>//protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)//{// moduleCatalog.AddModule<ModuleAProfile>();// moduleCatalog.AddModule<ModuleBProfile>();// base.ConfigureModuleCatalog(moduleCatalog);//}/// <summary>/// 创建模块/// </summary>protected override IModuleCatalog CreateModuleCatalog(){return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };}//protected override void OnStartup(StartupEventArgs e)//{// base.OnStartup(e);// if (new LoginWindow().ShowDialog() == true)// {// new MainWindow().ShowDialog();// }// Application.Current.Shutdown();//}}
}
ModuleAProfile
using ModuleA.Views;
using Prism.Ioc;
using Prism.Modularity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ModuleA
{/// <summary>/// 模块要实现 IModule 接口 /// </summary>public class ModuleAProfile : IModule{public void OnInitialized(IContainerProvider containerProvider){}/// <summary>/// 模块 服务注入/// </summary>/// <param name="containerRegistry"></param>public void RegisterTypes(IContainerRegistry containerRegistry){//导航containerRegistry.RegisterForNavigation<UserControl1>();}}
}
UserControl1.xaml
<UserControl x:Class="ModuleA.Views.UserControl1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ModuleA.Views"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><Grid><TextBlock Text="我是模块1" FontSize="50"/></Grid>
</UserControl>
AppData
/// <summary>
/// UI层公共数据
/// </summary>
public class AppData : BindableBase
{/// <summary>/// 这样就可以在事个UI层获取到AppData类的所有公共属性和方法/// </summary>public static AppData Instance = new Lazy<AppData>(() => new AppData()).Value;/// <summary>/// 主窗体/// </summary>public MainWindow MainWindow { get; set; }
}