Region-区域(默认和新增)适配器

Prism内置了几个区域适配器

ContentControlRegionAdapter

ItemsControlRegionAdapter

SelectorRegionAdapter

  • ComboBox

  • ListBox

  • Ribbon

  • TabControl

所以我们可以在ContentControl当中定义区域,

实际可以在任何元素上定义区域

如果定义的范围不再官方提供的默认适配器当中,则会引发异常。

 

自定义StackPanel区域适配器

一:新建一个类:StackPanelRegionAdapter 

 1 namespace BlankApp1.Views.StackPanel_Prism
 2 {
 3         //1、继承类;RegionAdapterBase<>
 4     public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
 5     {
 6         //2、生成构造函数
 7         public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
 8         {
 9         }
10         //3、生成实现抽像类
11         protected override void Adapt(IRegion region, StackPanel regionTarget)
12         {
13             //代码意思
14             //动态设置他内容的一个代码区域
15             //1、检测这个区域的视图动态变化
16             region.Views.CollectionChanged += (s, e) =>
17              {
18                  //判断他现在的行为是增加还是删除
19                  //如果是增加行为
20                  if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
21                  {
22                      //循环他的数组,获取所有控件
23                      foreach (FrameworkElement item in e.NewItems)
24                      {
25                          regionTarget.Children.Add(item);
26                      }
27 
28                  }
29 
30              };
31         }
32 
33         protected override IRegion CreateRegion()
34         {
35             //代码意思
36             //创建一个名为CreateRegionr的Region区域
37             //直接返回一个新的Region区域就可以了
38             return new Region();
39           
40         }
41     }
42 }

二:View代码中的MainWindows代码

 1         public MainWindow(IRegionManager regionManager)
 2         {
 3             InitializeComponent();
 4 
 5             //显示一个或循环显示多个
 6             for (int a = 0; a < 10; a++)
 7             {
 8                 regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewB));
 9             }
10             
11             RegionManager.SetRegionName(Ctr, "ContentRegion");
12 
13         }

三、App.xaml.cs

//4、初始化
protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
{
base.ConfigureRegionAdapterMappings(regionAdapterMappings);
regionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
}

        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }
        //4、初始化
        protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
        {
            base.ConfigureRegionAdapterMappings(regionAdapterMappings);
            regionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
        }

 

.

 

posted @ 2023-03-28 17:39  Windows窗  阅读(54)  评论(0编辑  收藏  举报