prism WPF 模块

模块 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)
        {
            //注册 导航 RegisterForNavigation
            containerRegistry.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)
        {
            //注册 导航 RegisterForNavigation
            containerRegistry.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; }
}
相关推荐
baivfhpwxf20232 小时前
prism WPF 对话框
c#·wpf
baivfhpwxf20236 小时前
WPF 登录页面
ui·wpf
baivfhpwxf202319 小时前
prism WPF 导航
wpf
军训猫猫头1 天前
87.在线程中优雅处理TryCatch返回 C#例子 WPF例子
开发语言·ui·c#·wpf
huizhixue-IT2 天前
华为存储考试内容&HCIP-Storage
wpf
桂月二二3 天前
实时事件流处理架构的容错设计
架构·wpf
源之缘-OFD先行者3 天前
GMap.NET + WPF:构建高性能 ADS-B 航空器追踪平台
.net·wpf·ads-b
Marzlam3 天前
WPF学习路线
wpf
weixin_535455793 天前
WPF设计学习记录滴滴滴2
学习·wpf