Prism is a lightweight WPF framework used to build a composite WPF Application and try to separate the Ui and logic as much as possible. The process of understanding this framework is also a process of understanding the design model.
Prism Link: http://compositewpf.codeplex.com/
Figure 1
1. It is the architecture diagram of Prism. We can see that multiple design modes are used. This article expands the Adapter modes used in Shell and Region.
Adapter mode:
Link: http://www.cnblogs.com/cjfwu/archive/2013/03/23/2977878.html
The definition and implementation of the Adapter mode are described in detail above.
In the Prism Bootstrapper main Bootstrap class, the following controls are registered in the assumeregionadaptermappings virtual method to provide adaptive conversion for the relevant classes:
- ContentControl
- TabControl (Sliverlight)
- Seletor
- ItemControl
Implement control adaptation in Shell. The Code is as follows:
ConfigureRegionAdapterMappings
protected virtual RegionAdapterMappings ConfigureRegionAdapterMappings() { RegionAdapterMappings regionAdapterMappings = ServiceLocator.Current.GetInstance<RegionAdapterMappings>(); if (regionAdapterMappings != null) {#if SILVERLIGHT regionAdapterMappings.RegisterMapping(typeof(TabControl), ServiceLocator.Current.GetInstance<TabControlRegionAdapter>());#endif regionAdapterMappings.RegisterMapping(typeof(Selector), ServiceLocator.Current.GetInstance<SelectorRegionAdapter>()); regionAdapterMappings.RegisterMapping(typeof(ItemsControl), ServiceLocator.Current.GetInstance<ItemsControlRegionAdapter>()); regionAdapterMappings.RegisterMapping(typeof(ContentControl), ServiceLocator.Current.GetInstance<ContentControlRegionAdapter>()); } return regionAdapterMappings; }
We have learned the ins and outs of the above, so that we can achieve function expansion through the rewrite and modification method.
If we need to add ListBox, the implementation is as follows:
1. inherit the IRegionAdapter Interface
public class ListBoxRegionAdapter : IRegionAdapter{ public IRegion Initialize(object regionTarget, string regionName) { ListBox listbox= regionTarget as ListBox; return new Region(); }}
2. Override the ConfigureRegionAdapterMappings method of Bootstrapper and register the ing relationship.
1 protected override RegionAdapterMappings ConfigureRegionAdapterMappings()2 {3 RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();4 regionAdapterMappings.RegisterMapping(typeof(ListBox), this.Container.Resolve<ListBoxRegionAdapter>());5 return regionAdapterMappings;6 }
This completes the adaptation extension.