03Prism WPF 入门实战 - Region

1.概要

源码及PPT地址:https://github.com/JusterZhu/wemail

视频地址:https://www.bilibili.com/video/BV1KQ4y1C7tg?sharesource=copyweb

(1)Prism概览

ac9913d4f18d35897785cd9067e6303d.png

  • Application:我们开发应用程序,初始化Bootstrapper。

  • Bootstrapper:是用来初始化应用程序级别的组件和服务,它也被用来配置和初始化module catalog和Shell 的View和View Model。

  • Modules:是能够独立开发、测试、部署的功能单元,Modules可以被设计成实现特定业务逻辑的模块(如Profile Management),也可以被设计成实现通用基础设施或服务的模块。

  • Shell是宿主应用程序(host application),modules将会被load到Shell中。Shell定义了应用程序的整体布局和结构,而不关心寄宿其中的Module,Shell通常实现通用的application service和infrastructure,而应用的逻辑则实现在具体的Module中,同时,Shell也提供了应用程序的顶层窗口。

  • Services:是用来实现非UI相关功能的逻辑,例如logging、exception management、data access。Services可以被定义在应用程序中或者是Module中,Services通常被注册在依赖注入容器中,使得其它的组件可以很容易的定位这个服务。

  • Container:注入服务、其他模块依赖。

111b4362f62dfda1fe34ecc99ab857a6.png

7e99e2998e33186f1f2c0169f221801a.png

(2)Region

  • Region是应用程序UI的逻辑区域(具体的表现为容器控件),Views在Region中展现,很多种控件可以被用作Region:ContentControl、ItemsControl、ListBox、TabControl。Views能在Regions编程或者自动呈现,Prism也提供了Region导航的支持。这么设计主要为了解耦让内容显示灵活具有多样性。

83ce1254a99da972fffe606bd21e6f01.png

在实战项目当中,需根据业务需求来划分Region。

ee856d9edbbffd98e02cbab30c0de05c.png

(3)RegionManager

RegionManager主要实现维护区域集合、提供对区域的访问、合成视图、区域导航、定义区域。

区域定义方式有两种:

  • Xaml实现

    <ContentControl x:Name=“ContentCtrl” prism:RegionManager.RegionName="ContentRegion" />
  • C#实现

    RegionManager.SetRegionName(ContentCtrl,”ContentRegion”);
    
    public MainWindowViewModel(IRegionManager regionManager)
    {
       _regionManager = regionManager;
       _regionManager.RegisterViewWithRegion("ContentRegion", typeof(MainWindow));
    }

(4)RegionAdapter

RegionAdapter(区域适配)主要作用为特定的控件创建相应的Region,并将控件与Region进行绑定,然后为Region添加一些行为。

因为并不是所有的控件都可以作为Region的,需要为需要定义为Region的控件添加RegionAdapter。一个RegionAdapter需要实现IRegionAdapter接口,如果你需要自定义一个RegionAdapter,可以通过继承RegionAdapterBase类来省去一些工作。

Prism为开发者提供了几个默认RegionAdapter:

  • ContentControlRegionAdapter:创建一个SingleActiveRegion并将其与ContentControl绑定

  • ItemsControlRegionAdapter:创建一个AllActiveRegion并将其与ItemsControl绑定

  • SelectorRegionAdapter:创建一个Region并将其与Selector绑定

  • TabControlRegionAdapter:创建一个Region并将其与TabControl绑定

2.详细内容

Region和RegionManager实战应用。

d28b6e5310e2a37511063679c7656c0f.png

(1)定义Region及选择好容器控件

<TabControl prism:RegionManager.RegionName="TabRegion" />

(2)ViewModel注册视图到TabRegion当中 public class MainWindowViewModel : BindableBase { //Region管理对象 private IRegionManager _regionManager; private string _title = "Prism Application";

public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    public MainWindowViewModel(IRegionManager regionManager)
    {
        //Prism框架内依赖注入的RegionManager
        _regionManager = regionManager;

        //在ContentRegion中注册视图TempView(TabItem1)
        _regionManager.RegisterViewWithRegion("TabRegion", typeof(TempView));

        //TabItem2
        _regionManager.RegisterViewWithRegion("TabRegion", typeof(Temp2View));

        //TabItem3
        _regionManager.RegisterViewWithRegion("WorkRegion", typeof(Temp3View));

        //对视图的访问、操作
        //var contentRegion = _regionManager.Regions["ContentRegion"];
        //contentRegion.Context
        //contentRegion.Remove()
        //contentRegion.Activate()
        //foreach (var item in contentRegion.ActiveViews)
        //{
        //    contentRegion.Activate(item);
        //}
    }
}

f3127a73750c1f2daca8e500910b4fc1.png

RegionAdapter实战应用。

如果在实际开发工作当中遇到了特殊场景需要而Prism并没有设置对应的RegionAdapter。这时候可以通过继承实现RegionAdapterBase内置对象来扩展一个新的RegionAdapter。

(1)实现一个新的RegionAdapter
/// <summary>
/// custom region adapter.
/// </summary>
public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
{
    public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
    {
    }

    protected override void Adapt(IRegion region, StackPanel regionTarget)
    {
        //该事件监听往StackPanel添加view时的操作
        region.Views.CollectionChanged += (sender, e) =>
        {
            //监听到增加操作时则往StackPanel添加Children,枚举出来的操作在后面一段代码中体现
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                regionTarget.Children.Clear();
                foreach (var item in e.NewItems)
                {
                    regionTarget.Children.Add(item as UIElement);
                }
            }
        };
    }

    protected override IRegion CreateRegion()
    {
        return new Region();
    }
}

// Summary:
//     Describes the action that caused a System.Collections.Specialized.INotifyCollectionChanged.CollectionChanged
//     event.
public enum NotifyCollectionChangedAction
{
    //
    // Summary:
    //     An item was added to the collection.
    Add,
    //
    // Summary:
    //     An item was removed from the collection.
    Remove,
    //
    // Summary:
    //     An item was replaced in the collection.
    Replace,
    //
    // Summary:
    //     An item was moved within the collection.
    Move,
    //
    // Summary:
    //     The contents of the collection changed dramatically.
    Reset
}

(2)在App.cs文件中注册新的RegionAdapter

public partial class App
{
    /// <summary>
    /// 应用程序启动时创建Shell
    /// </summary>
    /// <returns></returns>
    protected override Window CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
    }

    /// <summary>
    /// 配置区域适配
    /// </summary>
    /// <param name="regionAdapterMappings"></param>
    protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
    {
        base.ConfigureRegionAdapterMappings(regionAdapterMappings);


        //添加自定义区域适配对象,会自动适配标记上prism:RegionManager.RegionName的容器控件为Region
        regionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
    }
}

(3)在xaml中使用

<StackPanel prism:RegionManager.RegionName="StackPanelRegion"></StackPanel>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF Prism是微软新一代的界面开发框架,它具有丰富的功能和灵活的架构,广泛应用于桌面应用程序和Web应用程序的开发中。朝夕停车场项目是基于WPF Prism框架的一个实战项目,它实现了停车场的智能化管理,包括车辆进入和出场的自动识别,车位的实时监测和调度,以及财务管理和数据分析等功能。 该项目的源码包含了多个模块和组件,采用了MVVM架构和依赖注入技术,使代码组织和维护非常方便。项目中的主要模块包括: 1. Shell模块:该模块是整个应用程序的容器,它提供了主窗口和导航栏等界面组件,以及对其他模块的管理和协调。 2. Home模块:该模块实现了停车场的实时监控和调度功能,包括车位的占用和空闲状态显示,车辆进出场的记录和管理,以及停车位的预定和预约等功能。 3. Financial模块:该模块实现了停车场的财务管理和数据分析功能,包括车位租赁、停车费用计算和缴纳,以及停车场运营数据的统计和分析等功能。 4. Configuration模块:该模块实现了停车场的基础配置和参数管理功能,包括车位数量、收费标准和系统设置等功能。 5. Common模块:该模块包含了一些公共的模型和工具类,用于提供系统级别的服务和支持。 通过这个实战项目的源码学习,可以深入了解WPF Prism框架的应用及其MVVM架构和依赖注入的设计思想,也可以了解如何实现一个完整的智能化停车场管理系统。同时,该项目源码可以作为一个参考,通过在此基础上进行二次开发和定制,实现更加具体化的应用需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值