Learning design patterns from Prism: Adapter patterns

Source: Internet
Author: User

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.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.