Prism项目初始化与项目结构

发布时间 2023-09-03 11:36:42作者: ZHIZRL

Prism简介

Prism 框架适用于 WPF 和 Xamarin Forms 中构建松散耦合、可维护和可测试的应用程序。Prism 提供了一组设计模式的实现,这些设计模式有助于 编写结构良好且可维护的 xaml 应用程序,功能包括:

视图模型定位器(View Model Location)

MVVM(通知)

命令(Commands)

事件聚合器(EventAggregator)

模块化(Module)

依赖注入(Dependency Injection)

导航功能(Navigation)

对话服务(DialogService)

项目搭建

第一步 打开NuGet包管理器,添加Prism依赖包

第二步 搜索“Prism”

第三步 安装现“Prism.Core”和“Prism.Wpf”,IOC可选择“Prism.Unity”或“Prism.DryIoc”

第四步 改造WPF应用程序

生产力工具

官方文档:https://prismlibrary.com/docs/getting-started/productivity-tools.html
Prism 现在与 Visual Studio 和 Visual Studio for Mac 集成,为使用 Xamarin.Forms 创建 WPF、UWP 以及本机 iOS 和 Android 应用程序提供了高效的开发人员工作流。使用适用于所选 IDE 的代码段、项模板和项目模板快速启动 Prism 应用。
可用工具共有 3 种,分别是
Prism Template Pack
Prism Template Studio
Prism Extensibility Pack
Prism Template Pack
Prism Template Pack 包含用于生成 WPF 和使用棱镜的 Xamarin.Forms 应用程序的代码段、项模板和项目模板的集合。

Prism语法糖

propp - 属性,带有支持字段,依赖于 BindableBase

private string _fieldName;
public string PropertyName
{
get { return _fieldName; }
set { SetProperty(ref _fieldName, value); }
}

cmd - 使用 Execute 方法创建 DelegateCommand 属性

private DelegateCommand _fieldName;
public DelegateCommand CommandName =>
_fieldName ?? (_fieldName = new DelegateCommand(ExecuteCommandName));
void ExecuteCommandName()
{
}

cmdfull - 使用 Execute 和 CanExecute 方法创建 DelegateCommand 属性

private DelegateCommand _fieldName;
public DelegateCommand CommandName =>
_fieldName ?? (_fieldName = new DelegateCommand(ExecuteCommandName, CanExecuteCommandName));
void ExecuteCommandName()
{
}
bool CanExecuteCommandName()
{
return true;
}

cmdg - 创建泛型委托命令属性

private DelegateCommand<string> _fieldName;
public DelegateCommand<string> CommandName =>
_fieldName ?? (_fieldName = new DelegateCommand<string>(ExecuteCommandName));
void ExecuteCommandName(string parameter)
{
}

cmdgfull - 使用 Execute 和 CanExecute 方法创建泛型 DelegateCommand 属性

private DelegateCommand<string> _fieldName;
public DelegateCommand<string> CommandName =>
_fieldName ?? (_fieldName = new DelegateCommand<string>(ExecuteCommandName, CanExecuteCommandName));
void ExecuteCommandName(string parameter)
{
}
bool CanExecuteCommandName(string parameter)
{
return true;
}

Prism提供的WPF模板

**Prism Blank App :**这是一个项目模板,实质上是创建新的 WPF 外壳应用程序。它将有一个基本的引导程序,负责初始化应用程序并显示shell。它将有一个 MainWindow 和一个 MainWindowViewModel,分别位于 Views 和 ViewModels 文件夹中。
**Prism Module :**此项目模板将向解决方案中添加一个新项目,该项目将充当 Prism Module 。它将定义一个类,该类实现 IModule,其中包含两个用于视图和视图模型的空文件夹。

Cross Platform 

Prism ViewModel :从 BindableBase 派生并具有默认构造函数的视图模型。

WPF 

Prism UserControl :使用视图模型的用户控制
Prism Window :带视图的窗口模型

外部下载地址

https://marketplace.visualstudio.com/items?itemName=BrianLagunas.PrismTemplatePack 

在 Visual Studio 上的安装步骤

官方文档:https://www.prismsupport.com/documentation/Content/Topics-DocForm/Project_Wizard.htm

进入创建内,可以选择 Dryloc 和 Unity 两种模板,在这里选用 Dryloc 模板。

运行结果如下: