prism框架简介
Prism是一个用于在 WPF、Xamarin Form、Uno 平台和 WinUI 中构建松散耦合、可维护和可测试的 XAML 应用程序框架。Prism 提供了一组设计模式的实现,这些模式有助于编写结构良好且可维护的 XAML 应用程序,包括 MVVM、依赖注入、命令、EventAggregator 等。
他最初由Microsoft Patterns & Practices 团队构建的项目, 目的是帮助WPF开发人员以简化编写、维护和扩展来设计应用程序。
目前它是属于.NET基金会下的一个开源项目。
官方地址
https://github.com/PrismLibrary/Prism
安装
Prism 官方版本可在 NuGet 上获取
WPF | Xamarin Forms | Uno Platform |
---|---|---|
Prism.DryIoc | Prism.DryIoc.Forms | Prism.DryIoc.Uno |
Prism.Unity | Prism.Unity.Forms | |
Prism.Forms.Regions |
创建Prism应用程序
1 创建一个基于.NetFramework 或.Net的WPF应用程序。如下所示:
2 为当前应用程序安装prism,本文将以DryIoc容器
3 修改App.xaml文件,添加prism命名空间
- 继承由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>
- 打开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中可以使用。