当前位置:   article > 正文

WPF:Prism框架的简单使用方法和模块化_wpf prism 教程

wpf prism 教程

1、安装Prism

2、安装Prism模板包(使用VS2022)

3、安装完成之后退出VS2022,重新打开VS2022新建项目。

4、我创建WPF桌面程序需要使用的是“空应用程序”和“WPF控件”

5、至此就已经成功创建了一个使用Prism框架的WPF程序,并且已经实现了ViewModel与View的自动绑定。

6、实现按钮的命令绑定

界面代码

  1. <Window x:Class="PrismTestBlankApp.Views.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:prism="http://prismlibrary.com/"
  5. prism:ViewModelLocator.AutoWireViewModel="True"
  6. Title="{Binding Title}" Height="110" Width="120" >
  7. <Grid>
  8. <Grid>
  9. <ContentControl prism:RegionManager.RegionName="ContentRegion" />
  10. </Grid>
  11. <Grid>
  12. <Button Content="Button" Command="{Binding ButtonCommand}" HorizontalAlignment="Center" Margin="0,33,0,0" VerticalAlignment="Top"/>
  13. </Grid>
  14. </Grid>
  15. </Window>

 后台代码

  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using System;
  4. using System.Windows;
  5. namespace PrismTestBlankApp.ViewModels
  6. {
  7. public class MainWindowViewModel : BindableBase
  8. {
  9. private string _title = "Prism Application";
  10. public string Title
  11. {
  12. get { return _title; }
  13. set { SetProperty(ref _title, value); }
  14. }
  15. public MainWindowViewModel()
  16. {
  17. }
  18. //命令绑定
  19. private DelegateCommand _buttonCommand;
  20. public DelegateCommand ButtonCommand
  21. {
  22. get
  23. {
  24. if (_buttonCommand == null)
  25. {
  26. _buttonCommand = new DelegateCommand(ExecuteButtonCommand, CanExecuteButtonCommand);
  27. }
  28. return _buttonCommand;
  29. }
  30. }
  31. void ExecuteButtonCommand()
  32. {
  33. this.Title = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  34. }
  35. private bool CanExecuteButtonCommand()
  36. {
  37. return true;
  38. }
  39. }
  40. }

安装完Prism框架之后复制粘贴以上代码。

7、以上就是创建一个简单的WPF程序,仿照这个例子就可以往界面拖控件正常的写自己的代码了。

8、模块化,Prism提出了一个区域的概念,目前就简单理解为一个Grid为一个区域,如下:

上图中我在1~7步骤中生成的代码界面上添加了4个Grid,并使用红框中的代码将他们规定为4个区域并给区域起了名称,代码如下。

  1. <Window x:Class="PrismTestBlankApp.Views.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:prism="http://prismlibrary.com/"
  5. prism:ViewModelLocator.AutoWireViewModel="True"
  6. Title="{Binding Title}" Height="195" Width="225" >
  7. <Grid>
  8. <Grid.RowDefinitions>
  9. <RowDefinition Height="1*"/>
  10. <RowDefinition Height="1*"/>
  11. <RowDefinition Height="1*"/>
  12. <RowDefinition Height="1*"/>
  13. </Grid.RowDefinitions>
  14. <Grid Grid.Row="0">
  15. <ContentControl prism:RegionManager.RegionName="ContentRegion1" />
  16. </Grid>
  17. <Grid Grid.Row="1">
  18. <ContentControl prism:RegionManager.RegionName="ContentRegion2" />
  19. </Grid>
  20. <Grid Grid.Row="2">
  21. <ContentControl prism:RegionManager.RegionName="ContentRegion3" />
  22. </Grid>
  23. <Grid Grid.Row="3">
  24. <ContentControl prism:RegionManager.RegionName="ContentRegion4" />
  25. <Button Content="Button" Command="{Binding ButtonCommand}" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center"/>
  26. </Grid>
  27. </Grid>
  28. </Window>

9、创建多个用户控件,每个用户控件都是一个库文件,生成单独的dll,如下图。

 10、在主窗口中的App.xaml.cs代码文件中添加如下代码

  1. using Prism.DryIoc;
  2. using Prism.Ioc;
  3. using Prism.Modularity;
  4. using PrismTestBlankApp.Views;
  5. using System.Windows;
  6. namespace PrismTestBlankApp
  7. {
  8. /// <summary>
  9. /// Interaction logic for App.xaml
  10. /// </summary>
  11. public partial class App : PrismApplication
  12. {
  13. protected override Window CreateShell()
  14. {
  15. return Container.Resolve<MainWindow>();
  16. }
  17. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  18. {
  19. }
  20. protected override IModuleCatalog CreateModuleCatalog()
  21. {
  22. // return new DirectoryModuleCatalog() { ModulePath = @"C:\Users\Admin\Desktop\MyStudentFile\JiGuangBlankApp1\Module1\bin\Debug\net6.0-windows" };
  23. DirectoryModuleCatalog catelog = new DirectoryModuleCatalog();
  24. catelog.ModulePath = @"C:\Users\Admin\Desktop\MyStudentFile\PrismTestBlankApp\Models";
  25. return catelog;
  26. }
  27. }
  28. }

11、在每个用户控件的Model代码文件中添加如下代码

  1. using Module1.Views;
  2. using Prism.Ioc;
  3. using Prism.Modularity;
  4. using Prism.Regions;
  5. namespace Module1
  6. {
  7. public class Module1Module : IModule
  8. {
  9. public void OnInitialized(IContainerProvider containerProvider)
  10. {
  11. var regionManager = containerProvider.Resolve<IRegionManager>();
  12. regionManager.RegisterViewWithRegion("ContentRegion1", typeof(ViewA));
  13. }
  14. public void RegisterTypes(IContainerRegistry containerRegistry)
  15. {
  16. }
  17. }
  18. }

将该用户控件生成为一个dll文件,放到在主窗口指定的文件夹位置,运行主窗口该控件即可显示到主窗口的指定区域内。主窗口只显示用户控件,每个用户控件都具有自己的功能。想换哪个用户控件就直接换该控件的dll即可。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号