03Prism WPF 入门实战 - Region

1.概要

源码及PPT地址:

视频地址:source=copyweb

(1)Prism概览

 

 

  • 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:注入服务、其他模块依赖。

 

 

 

 

 

(2)Region

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

 

 

 

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

 

 

(3)RegionManager

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

区域定义方式有两种:

  • Xaml实现
    <ContentControl x:Name=“ContentCtrl” prism:RegionManager.RegionName="ContentRegion" />
  • C#实现
    RegionManager.SetRegionName(ContentCtrl,”ContentRegion”);

    publicMainWindowViewModel(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实战应用。

 

 

(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);
        //}
    }
}

 

 

 

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>
posted @ 2021-10-25 21:16  justerzhu  阅读(678)  评论(0编辑  收藏  举报