WPF框架Prism的使用

Prism简介

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

如果只是个人的一次性小型demo,是否使用Prism区别不是很大。
官方提供了很多示例帮助大家快速入门: 官方提供的示例.

Prism的主要功能特性

启动配置(bootstrapper)

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

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

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            
        }
    }
    /// <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,从而达到相似的效果

    /// <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来进行页面显示切换

<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”)

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框架提供的模块目录容器中之后,模块目录将会自动创建实例

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"
    <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的框架就是为了支持多个模块,多个页面,因此也专门提供了页面导航的功能,而不需要我们自己去实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值