WPF框架Prism的使用

Prism-WPF使用

Prism简介

Prism是适用于WPF的主流的MVVM设计框架之一,对于降低代码的耦合度,分布式开发比较有帮助,低耦合模块化的特性也能够让后期的代码维护工作量大大减少,适用于公司多人开发的大型项目。

如果只是个人的一次性小型demo,是否使用Prism区别不是很大。

官方提供了很多示例帮助大家快速入门: 官方提供的示例.

Prism的主要功能特性

启动配置(bootstrapper)

示例中给了这样一个例子,先设计一个Bootstrapper初始化Prism的容器,并进行服务注册,然后我们就能够利用Prism的框架进行编码。

csharp 复制代码
    class Bootstrapper : PrismBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            
        }
    }
csharp 复制代码
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var bootstrapper = new Bootstrapper();
            bootstrapper.Run();
        }
    }

但是可以看到,后续的例子中都没有显示地创建一个Bootstrapper文件,而是直接让启动文件App.xaml.cs的类App直接继承PrismApplication,从而达到相似的效果

csharp 复制代码
    /// <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)
        {

        }
    }

Region

Region 是使用Prism最先要了解的东西,Region是Prism提供的用来管理显示内容的控件,如下面的代码所示,我们需要在主页面(或其他页面文件)中定义一个Region,后续我们就可以通过控制region来进行页面显示切换

xml 复制代码
<Window x:Class="Regions.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        Title="Shell" Height="350" Width="525">
    <Grid>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" />
    </Grid>
</Window>

Module

Module就体现了Prism的模块化设计思想,一个大型项目,会有很多的模块,每个模块可以由不同的人进行开发,模块有自己的前端和后端,Prism提供了IoC容器,让开发者将模块注册进容器之后集中管理。

如下代码所示,Module需要继承IModule,需要实现OnInitialized和RegisterTypes两个函数,在初始化函数中通过IoC容器获取Region服务,并将模块对应的页面(ViewA)注册到对应的区域中("ContentRegion")

csharp 复制代码
using ModuleA.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Navigation.Regions;

namespace ModuleA
{
    public class ModuleAModule : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();
            regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            
        }
    }
}

在App.xaml.cs文件中添加如下代码,在ConfigureModuleCatalog函数中完成模块的注册,将模块信息注册到Prism框架提供的模块目录容器中之后,模块目录将会自动创建实例

csharp 复制代码
using Modules.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;
using System.Windows;

namespace Modules
{
    /// <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)
        {

        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<ModuleA.ModuleAModule>();
        }
    }
}

ViewModelLocator

我试用过的Caliburn.Micro以及Prism都提供了View和ViewModel的自动匹配功能,通过文件名(类名和命名空间,具体我没有去看是怎么检测匹配的)xxxView.xaml以及xxxViewModel.cs的方式使View和ViewModel完成自动匹配

我们来看一下具体是如何实现的

  1. xxxView.xaml以及xxxViewModel.cs的方式来创建我们的视图和视图模型

  2. 在视图的代码文件中添加prism:ViewModelLocator.AutoWireViewModel="True"

    xml 复制代码
    <Window x:Class="ViewModelLocator.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">
        <Grid>
            <ContentControl prism:RegionManager.RegionName="ContentRegion" />
        </Grid>
    </Window>

总结

了解上述几个功能后,我们就能够完成基本的页面开发了,下一章节我会加上以下内容

  1. Prism提供的Event功能,实现多个模块,或者页面之间的数据传递
  2. Prism提供的ICommand,提供触发UI空间的一些事件的实现
  3. Prism提供的窗口导航,Prism的框架就是为了支持多个模块,多个页面,因此也专门提供了页面导航的功能,而不需要我们自己去实现
相关推荐
Neil20132 小时前
ajax跨域请求
c#
唐青枫2 小时前
别把 Razor 当成几段 HTML:C#.NET Razor Pages、MVC 视图与实战详解
c#·.net
专注仿真8 小时前
CMO模型文档-活动单元基类
c#·模型·cmo·活动单元基类
我是苏苏11 小时前
Agent 开发实战 :C#实现语义检索!向量数据库Qdrant的下载安装及调试步骤
c#·ai编程
光泽雨12 小时前
IEnumerable<T> 详细讲解
c#·c
Lost of 程序猿12 小时前
.NET 10 船端单机部署与离线升级实战:从闪退事故到无感回滚
c#·asp.net·.net
淡海水13 小时前
11-04-Unity-Span-foreach-LINQ的分配与热路径边界
unity·c#·linq·foreach·span
唐青枫1 天前
对象到底住在哪里?C#.NET 托管堆从分配、GC 到内存排查
c#·.net
fogota1 天前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
灸木1 天前
C# 多线程与异步
c#