5.4 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T02:50:38.403475+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 01eecd59b616feb7 |
RegionAdapters
1. Purpose
This module provides custom Prism region adapters for integrating WPF StackPanel controls into the Prism region management system. Specifically, it enables views to be dynamically added to and removed from a StackPanel via Prism’s region abstraction, maintaining synchronization between the region’s view collection and the StackPanel’s Children collection. Two adapters exist: StackPanelRegionAdapter (general-purpose) and ViewerStackPanelRegionAdapter (likely intended for specialized viewer scenarios), both implementing identical adaptation logic but registered as distinct types—suggesting potential future divergence or explicit separation of concerns in the system’s region management strategy.
2. Public Interface
StackPanelRegionAdapter
-
Constructor:
public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
Initializes a new instance, passing the behavior factory to the baseRegionAdapterBase<StackPanel>constructor. -
Adaptmethod (override):
protected override void Adapt(IRegion region, StackPanel regionTarget)
Subscribes to theCollectionChangedevent ofregion.Views. OnAdd, appends newUIElementinstances toregionTarget.Children; onRemove, removes matching elements fromregionTarget.Children(if present). Does nothing ifregionisnull. -
CreateRegionmethod (override):
protected override IRegion CreateRegion()
Returns a newAllActiveRegioninstance, meaning all views in the region are kept active (not deactivated/destroyed on removal).
ViewerStackPanelRegionAdapter
-
Constructor:
public ViewerStackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
Identical toStackPanelRegionAdapter; passesregionBehaviorFactoryto the base constructor. -
Adaptmethod (override):
protected override void Adapt(IRegion region, StackPanel regionTarget)
Identical behavior toStackPanelRegionAdapter.Adapt. -
CreateRegionmethod (override):
protected override IRegion CreateRegion()
Identical behavior toStackPanelRegionAdapter.CreateRegion; returnsAllActiveRegion.
3. Invariants
regionTarget(theStackPanel) must be non-null whenAdaptis called (thoughregionmay benull, in which case the method exits early).- Views added to the region are appended to the end of
StackPanel.Childrenin the order they appear ine.NewItems. - Views removed from the region are removed from
StackPanel.Childrenonly if they are currently present in the collection (no exception thrown if missing). - The
AllActiveRegionreturned byCreateRegion()ensures all views remain active; no view deactivation occurs on removal—only visual removal from theStackPanel. - The
CollectionChangedevent handler is attached exactly once perAdaptcall (no duplicate subscriptions are prevented).
4. Dependencies
- Depends on:
Microsoft.Practices.Prism.Regions(Prism library), specificallyRegionAdapterBase<T>,IRegion,IRegionBehaviorFactory, andAllActiveRegion.System.WindowsandSystem.Windows.Controls(WPF), forStackPanel,UIElement, andCollectionChangedevent handling.
- Used by:
- Prism’s region management system (via
RegionAdapterMappingsor similar registration), whereStackPaneltargets are mapped to these adapters. - Likely consumed by UI modules that use
StackPanelas a layout container for dynamic view injection (e.g., tool windows, viewer panes). ViewerStackPanelRegionAdaptersuggests a dedicated path for viewer-related regions (e.g., medical imaging, CAD, or data visualization contexts), though its usage is not explicit in the source.
- Prism’s region management system (via
5. Gotchas
- No deduplication or ordering guarantees: If the same view is added multiple times to the region (e.g., via duplicate
Addnotifications), it will be added multiple times toStackPanel.Children. - No cleanup on adapter disposal: The
CollectionChangedevent handler is attached but never detached. IfAdaptis called multiple times on the sameStackPanelinstance (e.g., due to region recreation), multiple handlers will accumulate, causing duplicate additions/removals per event. AllActiveRegionbehavior may be unexpected: Views removed from the region remain in memory (asAllActiveRegiondoes not deactivate them), but they are only visually removed from theStackPanel. This could lead to memory leaks if views hold unmanaged resources.- Identical implementations: The near-identical code in
StackPanelRegionAdapterandViewerStackPanelRegionAdaptersuggests possible technical debt or incomplete refactoring. Without additional context (e.g., future plans for divergent behavior), the duplication is unjustified. - No handling for
ReplaceorResetactions: OnlyAddandRemoveactions are handled;NotifyCollectionChangedAction.ReplaceorResetwill be silently ignored, potentially leaving theStackPanelout of sync with the region.