上篇介紹了Region的基本應用,這篇接上篇,繼續介紹關於Region的相關應用—RegionAdapter
1.預先實現映射關系RegionAdapterMappings
RegionAdapterMappings對Control和RegionAdapter做了一個映射,這個映射是在Bootstrapper引導程序中完成的.
protected virtual RegionAdapterMappings ConfigureRegionAdapterMappings() { RegionAdapterMappings regionAdapterMappings = Container.TryResolve<RegionAdapterMappings>(); if (regionAdapterMappings != null) { regionAdapterMappings.RegisterMapping(typeof(Selector), this.Container.Resolve<SelectorRegionAdapter>()); regionAdapterMappings.RegisterMapping(typeof(ItemsControl), this.Container.Resolve<ItemsControlRegionAdapter>()); regionAdapterMappings.RegisterMapping(typeof(ContentControl), this.Container.Resolve<ContentControlRegionAdapter>()); } return regionAdapterMappings; }
這裡提供了三種控件支持,在上篇也有交代.RegionAdapter必須實現IRegionAdapter接口
2.在注冊Region時尋找RegionAdapter
<StackPanel cal:RegionManager.RegionName="MainRegion"/>
以上的定義是錯誤的,因為在RegionAdapterMappings無法找到StackPanel和RegionAdapter的映射關系.以上附加屬性的設置會嘗試執行以下代碼
protected virtual IRegion CreateRegion(DependencyObject targetElement, string regionName) { // Build the region IRegionAdapter regionAdapter = this.regionAdapterMappings.GetMapping(targetElement.GetType()); IRegion region = regionAdapter.Initialize(targetElement, regionName); return region; }
為了支持StackPanel,我們可以嘗試為StackPanel定義一個實現IRegionAdapter的區域適配器.