【从零开始学prism框架】

prism框架简介

Prism是一个用于在 WPF、Xamarin Form、Uno 平台和 WinUI 中构建松散耦合、可维护和可测试的 XAML 应用程序框架。Prism 提供了一组设计模式的实现,这些模式有助于编写结构良好且可维护的 XAML 应用程序,包括 MVVM、依赖注入、命令、EventAggregator 等。
他最初由Microsoft Patterns & Practices 团队构建的项目, 目的是帮助WPF开发人员以简化编写、维护和扩展来设计应用程序。
目前它是属于.NET基金会下的一个开源项目。

官方地址

https://github.com/PrismLibrary/Prism

安装

Prism 官方版本可在 NuGet 上获取

WPFXamarin FormsUno Platform
Prism.DryIocPrism.DryIoc.FormsPrism.DryIoc.Uno
Prism.UnityPrism.Unity.Forms
Prism.Forms.Regions

在这里插入图片描述

创建Prism应用程序

1 创建一个基于.NetFramework 或.Net的WPF应用程序。如下所示:
在这里插入图片描述
2 为当前应用程序安装prism,本文将以DryIoc容器
在这里插入图片描述
3 修改App.xaml文件,添加prism命名空间

  1. 继承由Application修改为PrismApplication
<prism:PrismApplication x:Class="WpfApp1.App"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             xmlns:prism="http://prismlibrary.com/"
             StartupUri="MainWindow.xaml">
    <Application.Resources>        
    </Application.Resources>
</prism:PrismApplication>
  1. 打开App.xaml.cs文件, 继承由Application修改为PrismApplication,实现CreateShell 与 RegisterTypes两个抽象方法
 public partial class App : PrismApplication
 {
      protected override Window CreateShell()
       {
           return Container.Resolve<MainWindow>();
       }

       protected override void RegisterTypes(IContainerRegistry containerRegistry)
       {

       }
}

1.CreateShell
该方法返回了一个Window类型的窗口, 其实就是返回应用程序的主窗口。

2.RegisterTypes
该方法用于在Prism初始化过程中, 我们定义自身需要的一些注册类型, 以便于在Prism中可以使用。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值