1、安装Prism包
2、安装Prism模板包(使用VS2022)
3、安装完成之后退出VS2022,重新打开VS2022新建项目。
4、我创建WPF桌面程序需要使用的是“空应用程序”和“WPF控件”
5、至此就已经成功创建了一个使用Prism框架的WPF程序,并且已经实现了ViewModel与View的自动绑定。
6、实现按钮的命令绑定
界面代码
<Window x:Class="PrismTestBlankApp.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="110" Width="120" >
<Grid>
<Grid>
<ContentControl prism:RegionManager.RegionName="ContentRegion" />
</Grid>
<Grid>
<Button Content="Button" Command="{Binding ButtonCommand}" HorizontalAlignment="Center" Margin="0,33,0,0" VerticalAlignment="Top"/>
</Grid>
</Grid>
</Window>
后台代码
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Windows;
namespace PrismTestBlankApp.ViewModels
{
public class MainWindowViewModel : BindableBase
{
private string _title = "Prism Application";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
public MainWindowViewModel()
{
}
//命令绑定
private DelegateCommand _buttonCommand;
public DelegateCommand ButtonCommand
{
get
{
if (_buttonCommand == null)
{
_buttonCommand = new DelegateCommand(ExecuteButtonCommand, CanExecuteButtonCommand);
}
return _buttonCommand;
}
}
void ExecuteButtonCommand()
{
this.Title = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private bool CanExecuteButtonCommand()
{
return true;
}
}
}
安装完Prism框架之后复制粘贴以上代码。
7、以上就是创建一个简单的WPF程序,仿照这个例子就可以往界面拖控件正常的写自己的代码了。
8、模块化,Prism提出了一个区域的概念,目前就简单理解为一个Grid为一个区域,如下:
上图中我在1~7步骤中生成的代码界面上添加了4个Grid,并使用红框中的代码将他们规定为4个区域并给区域起了名称,代码如下。
<Window x:Class="PrismTestBlankApp.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="195" Width="225" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<ContentControl prism:RegionManager.RegionName="ContentRegion1" />
</Grid>
<Grid Grid.Row="1">
<ContentControl prism:RegionManager.RegionName="ContentRegion2" />
</Grid>
<Grid Grid.Row="2">
<ContentControl prism:RegionManager.RegionName="ContentRegion3" />
</Grid>
<Grid Grid.Row="3">
<ContentControl prism:RegionManager.RegionName="ContentRegion4" />
<Button Content="Button" Command="{Binding ButtonCommand}" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center"/>
</Grid>
</Grid>
</Window>
9、创建多个用户控件,每个用户控件都是一个库文件,生成单独的dll,如下图。
10、在主窗口中的App.xaml.cs代码文件中添加如下代码
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using PrismTestBlankApp.Views;
using System.Windows;
namespace PrismTestBlankApp
{
/// <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 IModuleCatalog CreateModuleCatalog()
{
// return new DirectoryModuleCatalog() { ModulePath = @"C:\Users\Admin\Desktop\MyStudentFile\JiGuangBlankApp1\Module1\bin\Debug\net6.0-windows" };
DirectoryModuleCatalog catelog = new DirectoryModuleCatalog();
catelog.ModulePath = @"C:\Users\Admin\Desktop\MyStudentFile\PrismTestBlankApp\Models";
return catelog;
}
}
}
11、在每个用户控件的Model代码文件中添加如下代码
using Module1.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
namespace Module1
{
public class Module1Module : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
var regionManager = containerProvider.Resolve<IRegionManager>();
regionManager.RegisterViewWithRegion("ContentRegion1", typeof(ViewA));
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
}
将该用户控件生成为一个dll文件,放到在主窗口指定的文件夹位置,运行主窗口该控件即可显示到主窗口的指定区域内。主窗口只显示用户控件,每个用户控件都具有自己的功能。想换哪个用户控件就直接换该控件的dll即可。