📜 ⬆️ ⬇️

Simple RegionManager

Many people know the Prism library, but often all its extensive functionality is unnecessary. Sometimes you want to take advantage of some of the buns from this library.
It’s just impossible to tear out a small piece of code, then you have to create your own implementation with more or less approximate functionality.
RegionManager - one of the interesting buns, a simple implementation of which we consider

Immediately give an approximate piece of code using:

xaml -declared regions
<Grid x:Name="LayoutRoot" xmlns:service="clr-namespace:TestProgramm.Services"> <DockPanel LastChildFill="True"> <ContentControl DockPanel.Dock="Top" service:RegionManager.Region="TopPanel"/> <ContentControl DockPanel.Dock="Left" service:RegionManager.Region="LeftPanel"/> <ContentControl service:RegionManager.Region="Center"/> </DockPanel> </Grid> 

code- fill the central region
 SimpleIoc.Default.GetInstance<RegionManager>().Set("Center",SimpleIoc.Default.GetInstance<HomeView>()); 


Here and in the implementation itself I will use the MVVM Light Toolkit , but it will be small, which is not critical and will allow you not to be distracted.

')
Actually implementation of the RegionManager class:

 public class RegionManager { #region  private Dictionary <String, DependencyObject> Regions = new Dictionary<string , DependencyObject>(); #endregion /// <summary> ///   /// </summary> /// <param name="sender">    </param> /// <param name="name"> </param> public void Unregister(DependencyObject sender, string name) { if (Regions.Contains(new KeyValuePair<String, DependencyObject>(name, sender))) Regions.Remove(name); } /// <summary> ///   /// </summary> /// <param name="sender">    </param> /// <param name="name"> </param> public void Register(DependencyObject sender, string name) { if (sender.GetType().GetProperty( "Content" ) == null ) throw new ArgumentException(); Regions[name] = sender; } /// <summary> ///   /// </summary> /// <param name="name"> </param> /// <param name="element">,    </param> public void Set(String name, object element) { if (Regions.ContainsKey(name)) Regions[name].SetValue( ContentControl.ContentProperty, element); } #region  Region public static String GetRegion( DependencyObject obj) { return (String )obj.GetValue(RegionProperty); } public static void SetRegion( DependencyObject obj, String value) { obj.SetValue(RegionProperty, value); } public static readonly DependencyProperty RegionProperty = DependencyProperty.RegisterAttached("Region" , typeof( String), typeof(RegionManager ), new UIPropertyMetadata(PropertyChanged)); public static void PropertyChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e) { if (e.OldValue != null ) ServiceLocator.Current.GetInstance<RegionManager >().Unregister(sender, e.OldValue as String ); if (e.NewValue != null ) ServiceLocator.Current.GetInstance<RegionManager >().Register(sender, e.NewValue as String); } #endregion } 


ready.

Source: https://habr.com/ru/post/148566/


All Articles