Prism框架(1)—Region的使用

什么是区域(Region)?

在理解这个之前, 首先需要了解一下, 在最常见的开发模式当中, 我们去设计某个页面的时候, 实际上界面元素在设计的时候已经被固定。
因此我们可以为这个页面设计一些元素, 例如:

 MainWindow代码

<Window  
    x:Class="PrismApp.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:PrismApp"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="http://prismlibrary.com/"
    prism:ViewModelLocator.AutoWireViewModel="True"
    Title="{Binding Title}"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="9*" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <Button
                MinWidth="80"
                Margin="3"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewA"
                Content="打开模块A"
                FontSize="12" />
            <Button
                MinWidth="80"
                Margin="3"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewB"
                Content="打开模块B"
                FontSize="12" />
            <Button
                MinWidth="80"
                Margin="3"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewC"
                Content="打开模块C"
                FontSize="12" />
        </StackPanel>
        <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" />
    </Grid>
</Window>

1定义切换视图的按钮

 2.定义区域 

 <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" />

添加3个用户控件,用于显示到区域中

<UserControl x:Class="PrismApp.Views.ViewA"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:PrismApp.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid Background="AliceBlue">
        <TextBlock Text="ViewA" FontSize="90" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</UserControl>

另外连个视图添加相同

依赖注入

 App.Xaml.cs代码

public partial class App : PrismApplication
    {
        protected override Window CreateShell()//启动页
        {
            return Container.Resolve<Views.MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)//依赖注入
        {
            //注入视图
            containerRegistry.RegisterForNavigation<ViewA>();
            containerRegistry.RegisterForNavigation<ViewB>();
            containerRegistry.RegisterForNavigation<ViewC>();
        }
    }

注入视图

//注入视图
            containerRegistry.RegisterForNavigation<ViewA>();
            containerRegistry.RegisterForNavigation<ViewB>();
            containerRegistry.RegisterForNavigation<ViewC>();

注册区域

MainWindowViewModle代码

namespace PrismApp.ViewModels
{
    internal class MainWindowViewModel:BindableBase
    {
        private string _title="Prism App";   //标题
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title,value); }
        }   

        public DelegateCommand<string> OpenCommand { get; private set; }

        private readonly IRegionManager regionManager;

        public MainWindowViewModel(IRegionManager regionManager)  //注册区域
        {
            OpenCommand =new DelegateCommand<string>(Open);
            this.regionManager = regionManager;

            //#region 注册区域
            //regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));
            //regionManager.RegisterViewWithRegion("ContentRegion2", typeof(ViewB));
            //regionManager.RegisterViewWithRegion("ContentRegion3", typeof(ViewC));
            //#endregion
        }

        private void Open(string obj)//页面切换方法
        {
            regionManager.Regions["ContentRegion"].RequestNavigate(obj);
        }
    }
}

注册区域

        private readonly IRegionManager regionManager;
        public MainWindowViewModel(IRegionManager regionManager)  //注册区域
        {
            this.regionManager = regionManager;
        }

视图切换命令

 public DelegateCommand<string> OpenCommand { get; private set; }//定义命令
OpenCommand =new DelegateCommand<string>(Open);//初始化
private void Open(string obj)//页面切换方法
{
    regionManager.Regions["ContentRegion"].RequestNavigate(obj);
}

*初始化显示注册的区域(可以设置多个区域显示)

//#region 初始化注册区域
regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));
//regionManager.RegisterViewWithRegion("viewB", typeof(ViewB));
//regionManager.RegisterViewWithRegion("viewC", typeof(ViewC));
//#endregion

注意事项

细心的网友可能已经看到了, 上面的代码中, 为3个ContentControl注册了对应的区域, 然后显示3个自定义的控件。
那么, 是不是同样可以在其它控件元素上注册Region?
是的, 在Prism中, 控件都支持注册Region, 只是有些控件需要自己实现一个RegionAdapters(区域适配器)

什么是RegionAdapters?

假设在应用程序的某个区域, 需要显示我们定义的视图,这个时候实际上利用了RegionAdapter。
该类负责将传入我们定义的视图到指定的Region当中。

Prism提供了许多内置得RegionAdapter
ContentControlRegionAdapter
ItemsControlRegionAdapter
SelectorRegionAdapter
- ComboBox
- ListBox
- Ribbon
- TabControl

注:除此之外, 如果想要实现控件作用域Region, 则必须创建自己的自定义Region, 因为一旦你不这么做, 则会引发异常。

自定义StackPanel 区域适配器

StackPanelRegionAdapter类代码

namespace PrismApp
{
    /// <summary>
    /// 自定义StackPanel区域
    /// </summary>
    internal class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="regionBehaviorFactory"></param>
        public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
        {
        }
        /// <summary>
        /// 动态添加内容,检测区域有控件添加
        /// </summary>
        /// <param name="region"></param>
        /// <param name="regionTarget"></param>
        protected override void Adapt(IRegion region, StackPanel regionTarget)
        {
            region.Views.CollectionChanged += (s, e) =>
              {
                  if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                  {
                      foreach (FrameworkElement item in e.NewItems)
                      {
                          regionTarget.Children.Add(item);
                      }
                  }
              };
        }
        /// <summary>
        /// 创建Region区域
        /// </summary>
        /// <returns></returns>
        protected override IRegion CreateRegion()
        {
            return new Region();
        }
    }
}

App.Xaml.cs中初始化自定义StackPanel区域适配器

/// <summary>
/// 初始化自定义StackPanel
/// </summary>
/// <param name="regionAdapterMappings"></param>
protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
        {
            base.ConfigureRegionAdapterMappings(regionAdapterMappings);
            regionAdapterMappings.RegisterMapping(typeof(StackPanel),                              
            Container.Resolve<StackPanelRegionAdapter>());
        }

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: WPF Prism框架是一个面向对象的框架,用于开发模块化、可扩展的WPF应用程序,它基于MVVM设计模式和依赖注入技术。该框架的主要目的是能够轻松地实现可插拔的模块,公共的服务、组件和工具类的共享,同时也提供了灵活的路由、事件聚合、模块加载、导航和命令处理等机制。使用WPF Prism框架可以快速地开发出灵活的WPF应用程序,从而提高代码质量和开发效率,减少代码的维护难度。 WPF Prism框架具有以下的特点: 1. 扩展性:可以轻松地添加新的模块、服务和组件,同时也可以快速替换现有的组件。 2. 可重用性:提供了丰富的公共组件、服务和工具类,从而可以提高代码的可重用性。 3. 灵活性:提供了灵活的路由、事件聚合、模块加载和导航等机制,能够更好地满足应用程序的需求。 4. 易用性:提供了一套完整的MVVM设计模式和依赖注入技术的实践方案,从而能够更好地组织应用程序的逻辑。 总之,WPF Prism框架是一个强大的工具,能够让我们更好地开发WPF应用程序,提高代码质量和开发效率,实现可插拔的模块化和可扩展性,同时也具有灵活性和易用性。 ### 回答2: WPF Prism框架是一种面向MVVM模式的开源框架,它帮助开发人员使用模块化的方式构建可扩展、可重用和易于维护的WPF应用程序。该框架主要由Microsoft和模式仲裁者团队开发和维护,它借鉴了许多现代的软件开发理念,比如IoC容器、依赖注入和事件聚合器等。 WPF Prism框架的核心思想是将应用程序分解为许多可独立维护和扩展的模块。这些模块可以基于业务逻辑、UI、数据或任何其他特征进行分组。在该框架中,模块由各种名为“组件”的构建块组成。这些组件包括视图(View)、视图模型(ViewModel)、服务(Service)、模型(Model)等。通过基于这些组件的开发,可以实现具有高度可伸缩性和可重用性的应用程序。 为了实现这种模块化开发和组件化架构,WPF Prism框架提供了一些重要的工具和特性。例如,在该框架中可以使用依赖注入容器(如Unity)来管理组件及其依赖关系。此外,该框架还提供了一些基于事件的消息机制,可方便地实现模块间的交互和通信。 总体来说,WPF Prism框架是一种利用开源技术实现企业级应用程序开发的最佳选择。它具有良好的模块化、组件化和可扩展性特性,可以在实现复杂WPF应用程序时提高开发效率和代码质量。 ### 回答3: WPF Prism是一个基于WPF的框架,它为大型应用程序提供了一种组织、设计和部署的方式。它旨在帮助开发者构建可扩展、可维护和可测试的WPF应用程序。 WPF Prism采用了面向模块的编程思想,它将整个应用程序划分为多个模块,每个模块都包含自己的逻辑和用户界面。这种模块化设计使得应用程序易于扩展和维护,同时也简化了开发流程。 WPF Prism同时提供了一组强大的工具和功能,如依赖注入、命令模式和事件聚合等,这些功能让WPF应用程序更加易于开发和测试。它还提供了一个强大的导航和区域管理系统,开发者可以使用这些系统来管理不同部分的用户界面和功能。 总之,WPF Prism是一个优秀的框架,它为开发者提供了全面的工具和功能,使得构建WPF应用程序变得更加容易和高效。它的结构良好、可扩展性强,而且可以充分利用WPF的强大功能。无论是大型企业应用程序还是小型桌面应用程序,WPF Prism都是一个理想的选择。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值