当前位置: 首页 > news >正文

.NET_Prism基本项目创建

Prism简述

  1. 模块(Module):独立的功能单元,可动态加载。
  2. 依赖注入(Dependency Injection,DI):通过 IoC 容器(如 Unity、Autofac)管理模块之间的依赖关系。(一个类型是具体的(非抽象的)并且有一个公共构造函数,容器在需要的时候就会自动创建它的实例,即使你没有显式地在容器中注册该类型)
  3. 区域管理(Region Management):使用 IRegionManager 进行动态 UI 组件加载。
  4. 事件聚合(Event Aggregator):使用 IEventAggregator 进行模块间通信。

Prism 项目创建(MVVM)

1. 安装Prism

Install-Package Prism.Unity

关于Prism.Core与Prism.Unity的关系

  • Prism.Core 是 Prism 的基础库,提供核心功能(事件聚合、命令、MVVM 基础功能等),适用于所有平台(如 WPF、Xamarin)。
  • Prism.Unity 依赖于 Prism.Core,并为 Prism 提供 Unity 依赖注入支持,模块化开发、区域管理,适用于 WPF 应用。

2. 创建启动类

2.1 修改App.xaml
① 添加命名空间xmlns:prism="http://prismlibrary.com/"
② 将标签名修改为prism:PrismApplication
③ 删除StartUri

示例代码

<prism:PrismApplication x:Class="YourApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:YourApp"
             xmlns:prism="http://prismlibrary.com/">
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>

2.2 修改App.xaml.cs
① 实现PrismApplication类
② 重写CreateShell()ConfigureModuleCatalog(IModuleCatalog moduleCatalog
③ (可选)重写RegisterTypes(IContainerRegistry containerRegistry)

注:App.xaml.cs中所做的设置均为全局设置
注:App.xaml.cs实例由WPF框架创建,无法通过构造函数依赖注入。若想获取IRegionManager可通过 var regionManager = Container.Resolve<IRegionManager>() 获取,IEventAggregator同理

示例代码

public partial class App : PrismApplication
{
    /**
     * 此处设置均为全局设置
     */

    /**
     * 解析并返回主窗口
     */
    protected override Window CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    /**
     * 注册服务: 依赖注入或导航
     * 例如: containerRegistry.Register<IMessageService, MessageService>();
     * 例如: containerRegistry.RegisterSingleton<IMessageService, MessageService>();
     * 例如: containerRegistry.RegisterForNavigation<ViewA>();
     * 
     * IRegionManager与IEventAggregator已由Prism注册
     */
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {

    }

    /**
     * 模块注册
     * (
     *  按需加载
     *  moduleCatalog.AddModule<ModuleA>(InitializationMode.OnDemand);
     *  (在其他地方)
     *  private readonly IModuleManager _moduleManager;
     *  public MyClass(IModuleManager moduleManager)
     *  {
     *      _moduleManager = moduleManager;
     *  }
     *  ......
     *  _moduleManager.LoadModule("ModuleA");
     * )
     */
    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        moduleCatalog.AddModule<ModuleA>();
        moduleCatalog.AddModule<ModuleB>();
    }

}

3. 创建主窗口

① 添加命名空间 xmlns:prism="http://prismlibrary.com/"
② 添加区域(Region)以动态加载View(例如:<ContentControl prism:RegionManager.RegionName="MainRegion"/>)

注:主窗口至少需要一个区域

示例代码

<Window x:Class="YourApp.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:YourApp"
        xmlns:prism="http://prismlibrary.com/"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <ContentControl prism:RegionManager.RegionName="MainRegion"/>
        <ContentControl prism:RegionManager.RegionName="MessageRegion" Grid.Column="1"/>
    </Grid>
</Window>

4. 创建模块

① 实现IModule接口
② 实现OnInitialized(IContainerProvider containerProvider)与RegisterTypes(IContainerRegistry containerRegistry)

注:模块中所做的设置仅在模块被加载时才生效

示例代码

public class ModuleA : IModule
{
    /**
    * 模块中的一切设置均局限于本模块内 仅在模块被加载时生效
    */
    private readonly IRegionManager _regionManager;

    public ModuleA(IRegionManager regionManager)
    {
        _regionManager = regionManager;
    }

    /**
     * 模块初始化逻辑
     */
    public void OnInitialized(IContainerProvider containerProvider)
    {
        // 将 ViewA 区域注入至 MainRegion(或者说将 MainRegion 的初始视图设置为 ViewA)
        _regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewA));
    }

    /**
     * 注册服务: 依赖注入或导航
     */
    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        // 注册导航服务
        containerRegistry.RegisterForNavigation<ViewA>();
        containerRegistry.RegisterForNavigation<ViewB>();
    }
}

5. 创建View与ViewModel 

说明:
① View与ViewModel的自动绑定规则:
        View位于Views文件夹下,ViewModel位于ViewModels文件夹下
        Views文件夹与ViewModels文件夹位于同一命名空间
        ViewModel的命名为xxxViewModel(xxx为View的命名)
② BindableBase类:类继承BindableBase类后,通过SetProperty(ref _param, value)可自动处理 INotifyPropertyChange

注:propp可快捷创建使用SetProperty(ref _param, value)的属性

View示例代码

<UserControl x:Class="YourApp.Views.ViewA"
             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:YourApp.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel>
            <TextBlock Text="This is ViewA" FontSize="24" Panel.ZIndex="-1"/>
            <TextBlock Text="{Binding Message}" FontSize="24" Panel.ZIndex="-1"/>
        </StackPanel>
        <Button Content="Aletr" Height="100" Width="300" FontSize="24" Command="{Binding AlertCommand}"></Button>
        <Button Content="Change To ViewB" Command="{Binding ChangeCommand}" Height="100" Width="300" FontSize="24" Grid.Row="1" ></Button>
    </Grid>
</UserControl>

ViewModel示例代码

public class ViewAViewModel : BindableBase
{
    private readonly IRegionManager _regionManager;
    private readonly IEventAggregator _eventAggregator;

    public ViewAViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
    {
        _regionManager = regionManager;
        _eventAggregator = eventAggregator;
        _eventAggregator.GetEvent<MessageEvent>().Subscribe(ReceiveMessage);
    }

    public ICommand AlertCommand
    {
        get => new DelegateCommand(Alert);
    }
    public ICommand ChangeCommand
    {
        get => new DelegateCommand(Change);
    }

    private string _message = "None";
    public string Message
    {
        get => _message;
        set => SetProperty(ref _message, value);
    }
    
    private void Alert()
    {
        new Window().Show();
    }
    private void Change()
    {
        // 将MainRegion区域导航至ViewB
        _regionManager.RequestNavigate("MainRegion", "ViewB");
    }

    private void ReceiveMessage(string message)
    {
        Message = message;
    }
}

至此基本的Prism项目就创建完成

相关文章:

  • Java实体类转JSON时如何避免null值变成“null“?
  • TypeORM 和 Mongoose 是两种非常流行的 ORM 工具
  • Kubernetes pod 控制器 之 Deployment
  • pytorch中的基础数据集
  • CSS引入方式、字体与文本
  • Flask中使用WTForms处理表单验证
  • 前端学习记录:解决路由缓存问题
  • 东芝2323AMW复印机安装纸盒单元后如何添加配件选项
  • 【商城实战(38)】Spring Boot:从本地事务到分布式事务,商城数据一致性的守护之旅
  • 嵌入式系统中的Board Support Package (BSP)详解:以Xilinx Zynq为例
  • AndroidStudio+Android8.0下的Launcher3 导入,编译,烧录,调试
  • BSP、设备树和HAL的关系:以Xilinx Zynq为例与PC BIOS的对比
  • nginx请求限流设置:常见的有基于 IP 地址的限流、基于请求速率的限流以及基于连接数的限流
  • 结构体定义与应用
  • 查看分析日志文件、root密码不记得了,那应该怎么解决这些问题
  • Web开发-PHP应用鉴别修复AI算法流量检测PHP.INI通用过滤内置函数
  • SGMEA: Structure-Guided Multimodal Entity Alignment
  • 【AWS入门】AWS云计算简介
  • 文档搜索引擎
  • PyTorch使用-张量数值计算
  • WordPress怎么去掉底部/seo推广专员工作好做吗
  • 如何用api方式做网站/加强服务保障满足群众急需m
  • 网站开发合同知识产权/seo网站关键字优化
  • 营销网站 需求说明书/各行业关键词
  • 国外网站配色/福州网站优化公司
  • 涪城移动网站建设/深圳关键词优化软件