• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C# WPF MVVM模式Prism框架从零搭建(经典)

武飞扬头像
DotNet工控上位机编程
帮助1

01前言

目前最新的PRISM的版本是8.1.97,本节以6.3.0.0 讲解,可以在Github上获取PRISM的源码。

Prism要用到IOC容器,提供选择的有Unity和MEF,这里我分别采用MEF和unity去做。

02安装库

在nuget上安装Prism相关常用的库

学新通

03项目搭建

step1:新建解决方案:我这里命名为PrismFrameTest;

step2:删除MainWindow.xaml,删除App.xaml中启动引导

 StartupUri="MainWindow.xaml"

然后在App.xaml.cs新建程序入口

  1.  
    protected override void OnStartup(StartupEventArgs e)
  2.  
    {
  3.  
    base.OnStartup(e);
  4.  
    MyBootstrapper bootStrapper = new MyBootstrapper();
  5.  
    bootStrapper.Run(true);
  6.  
    }

新建引导类MyBootstrapper.cs,需要继承基类Prism.Mef库下的基类MefBootstrapper

方式1 采用mef

  1.  
     
  2.  
    public class MyBootstrapper : MefBootstrapper
  3.  
    {
  4.  
    protected override DependencyObject CreateShell()
  5.  
    {
  6.  
    return this.Container.GetExportedValue<MyShellView>();
  7.  
    }
  8.  
    protected override void InitializeShell()
  9.  
    {
  10.  
    base.InitializeShell();
  11.  
    Application.Current.MainWindow = (MyShellView)this.Shell;
  12.  
    Application.Current.MainWindow.Show();//Show主窗口,但content内没有内容,只有当调用Module中的Initialize()方法后才将HelloWorldView显示出来。
  13.  
    }
  14.  
    protected override void ConfigureAggregateCatalog()
  15.  
    {
  16.  
    base.ConfigureAggregateCatalog();
  17.  
    this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MyBootstrapper).Assembly));
  18.  
    this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(PrismModuleLeft.ModuleLeftViewModel).Assembly));//注册模块
  19.  
    //this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleB.ModuleBViewModel).Assembly));
  20.  
    }
  21.  
    protected override IModuleCatalog CreateModuleCatalog()
  22.  
    {
  23.  
    // When using MEF, the existing Prism ModuleCatalog is still the place to configure modules via configuration files.
  24.  
    return new ConfigurationModuleCatalog();
  25.  
    }
  26.  
     
  27.  
    }
学新通

方式2 采用unity

  1.  
    public class MyBootstrapper : UnityBootstrapper
  2.  
    {
  3.  
    protected override DependencyObject CreateShell()
  4.  
    {
  5.  
    return Container.Resolve<MyShellView>();
  6.  
    }
  7.  
     
  8.  
    protected override void InitializeShell()
  9.  
    {
  10.  
    base.InitializeShell();
  11.  
     
  12.  
    Application.Current.MainWindow = (MyShellView)this.Shell;
  13.  
    Application.Current.MainWindow.Show();//Show主窗口,但content内没有内容,只有当调用Module中的Initialize()方法后才将HelloWorldView显示出来。
  14.  
    }
  15.  
     
  16.  
    protected override void ConfigureModuleCatalog()
  17.  
    {
  18.  
    base.ConfigureModuleCatalog();
  19.  
     
  20.  
    ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
  21.  
    moduleCatalog.AddModule(typeof(PrismModuleLeft.ModuleLeftViewModel));//注册模块
  22.  
    }
  23.  
     
  24.  
    }
学新通

step3:

然后新建一个xaml窗体MyShellView.xaml,将窗体分为左右两部分

这里cal:RegionManager.RegionName是一个依赖属性,我们将它与ItemsControl控件相关联,MainRegion就是一个占位符。

  1.  
    <ItemsControl cal:RegionManager.RegionName="RegionLeft" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  2.  
    <ItemsControl cal:RegionManager.RegionName="RegionRight" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1"/>

对应的cs中将类标注为  [Export]

step4:新建类库PrismModuleLeft

类库中新建ModuleLeftView.xaml

关于事件绑定:(在下面代码中两种方式都列出来了)

①控件继承自ButtonBase、MenuItem类,比如:Button、RadioButton、Hyperlink、MenuItem……这种情况下,由于Prism已经帮我们实现了这些控件的Command属性,可以直接绑定Command属性来完成Click事件到ViewModel的绑定:

②ListView、ListBox、DropDownList等等大部分没有Click事件的控件。这时候,当我们要实现SelectedItemChanged、SelectionChanged等常用事件的时候,使用Expression Blend附带的System.Windows.Interactivity.dll文件,它使用interaction trigger和InvokeCommandAction behavior来帮助我们直接绑定控件的事件。

需要引用

  1.  
     
  2.  
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  1.  
    <Grid>
  2.  
    <Grid.RowDefinitions>
  3.  
    <RowDefinition/>
  4.  
    <RowDefinition/>
  5.  
    </Grid.RowDefinitions>
  6.  
    <TextBlock Foreground="Red" FontSize="20" Text="{Binding TxtLabel}" Background="Gray" Grid.Row="0"/>
  7.  
    <Button Background="LightCyan" Name="CreateRecipe" Command="{Binding CreateRecipeCommand}" Content="BtnCtr" FontSize="20" Grid.Row="1">
  8.  
    <i:Interaction.Triggers >
  9.  
    <i:EventTrigger EventName="PreviewKeyDown">
  10.  
    <i:InvokeCommandAction Command="{Binding KeyUpEventCommand}" />
  11.  
    </i:EventTrigger>
  12.  
    </i:Interaction.Triggers>
  13.  
    </Button>
  14.  
    </Grid>

对应的cs中:

  1.  
    [Export]
  2.  
    public partial class ModuleLeftView : UserControl
  3.  
    {
  4.  
    private readonly IRegionViewRegistry regionViewRegistry;
  5.  
    public ModuleLeftView()
  6.  
    {
  7.  
    InitializeComponent();
  8.  
    this.DataContext = new ModuleLeftViewModel(regionViewRegistry);
  9.  
    }
  10.  
    }

step4:ModuleLeftViewModel中:

  1.  
     
  2.  
    using Prism.Commands;
  3.  
    using Prism.Mef.Modularity;
  4.  
    using Prism.Modularity;
  5.  
    using Prism.Mvvm;
  6.  
    using Prism.Regions;
  7.  
    using PropertyChanged;
  8.  
    using System.ComponentModel.Composition;
  9.  
    using System.Windows;
  10.  
    using System.Windows.Input;
  11.  
     
  12.  
    namespace PrismModuleLeft
  13.  
    {
  14.  
    [AddINotifyPropertyChangedInterface]
  15.  
    [ModuleExport("ModuleLeftViewModel", typeof(ModuleLeftViewModel), InitializationMode = InitializationMode.WhenAvailable)]
  16.  
    public class ModuleLeftViewModel : BindableBase,IModule
  17.  
    {
  18.  
    private readonly IRegionViewRegistry regionViewRegistry;
  19.  
    public ICommand CreateRecipeCommand { get; set; }
  20.  
    public DelegateCommand<KeyEventArgs> KeyUpEventCommand { get; private set; }
  21.  
    public string TxtLabel { get; set; } = "Hello! I am ModuleA";
  22.  
    public void KeyUpEventHandler(KeyEventArgs args)
  23.  
    {
  24.  
    MessageBox.Show("PrismCTR");
  25.  
    }
  26.  
    public void Initialize()
  27.  
    {
  28.  
    regionViewRegistry.RegisterViewWithRegion("RegionLeft", typeof(ModuleLeftView));
  29.  
     
  30.  
    }
  31.  
    [ImportingConstructor]
  32.  
    public ModuleLeftViewModel(IRegionViewRegistry registry)
  33.  
    {
  34.  
    this.regionViewRegistry = registry;
  35.  
    CreateRecipeCommand = new DelegateCommand(() => CreateRecipe());
  36.  
    }
  37.  
    public void CreateRecipe()
  38.  
    {
  39.  
    TxtLabel = "this is my first prism test example";
  40.  
    MessageBox.Show(TxtLabel);
  41.  
    }
  42.  
    }
  43.  
    }
学新通

04总结

这个时候我们来对PRISM的基础架构做一个简单的总结:

Shell: 主窗口,他的功能都是通过Module来实现的;

Bootstrapper: 应用程序的入口点;

Region: 内容区域,类似于一个占位符

Module: 真正实现业务功能的东西,是View,数据,模型组成的集合;

Prism是个非常强大的wpf mvvm模式框架,它使用依赖注入,控制反转容器来帮助我们解决团队合作的松耦合问题。

05结果演示

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfkcaf
系列文章
更多 icon
同类精品
更多 icon
继续加载