Prism code sample learning: 03-Custom Regions

1 minute read

Prism code sample learning: 03-Custom Regions

Introduction

This is a continuation of the following article.
https://qiita.com/mngreen/items/14ad9c89fef988d7c4e5

03-CustomRegions

In this sample, RegionAdapterBase class is used to give the name of the region to the existing control.

<Window x:Class="Regions.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        Title="Shell" Height="350" Width="525">
    <Grid>
        <StackPanel prism:RegionManager.RegionName="ContentRegion" />
    </Grid>
</Window>
    public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
    {
        public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {

        }

        protected override void Adapt(IRegion region, StackPanel regionTarget)
        {
            region.Views.CollectionChanged += (s, e) =>
            {
                if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                {
                    foreach (FrameworkElement element in e.NewItems)
                    {
                        regionTarget.Children.Add(element);
                    }
                }

                //handle remove
            };
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }
    }

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        ... (abridgement) ...

        protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
        {
            base.ConfigureRegionAdapterMappings(regionAdapterMappings);
            regionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
        }
    }

–Region mapping is registered by ConfigureRegionAdapterMappings method.
–The ConfigureRegionAdapterMappings method is called inside the Bootstrap.Run method
–RegionAdapterBase \ <T > class is here [https://github.com/PrismLibrary/Prism/blob/master/src/Wpf/Prism.Wpf/Regions/RegionAdapterBase.cs)
–Adapt method is called in Initialize method
–It seems that the processing at the time of addition / deletion can be implemented for the child elements added to View by the Adapt method.
–Called by the Initialize method implemented in this class in the IRegionAdapter.Initialize method.
–The above interface + method name defines Implementation of explicit interface
–The CreateRegion method is also called inside the Initialilze method.
–Click here for AllActiveRegion class (https://github.com/PrismLibrary/Prism/blob/master/src/Wpf/Prism.Wpf/Regions/AllActiveRegion.cs)
-Region has an active state and an inactive state
–I couldn’t understand the meaning of the property by looking at Definition destination. However, as far as I can guess from the text, can performance be improved by managing the active Region or the state? I think that the.

in conclusion

This time, I mainly read the source code of the RegionAdapterBase class.
Some implementations are unknown at this stage, so I’ll continue reading to clarify them.
Next time, I will look at 04-View Discovery.