From 3e214f3af7aa8e499c6e707011693dc84be5b8eb Mon Sep 17 00:00:00 2001 From: Damian Suess Date: Sat, 14 Dec 2024 14:33:47 -0500 Subject: [PATCH] Reduced duplicated code between Prism.Avalonia and Prism.Wpf `Navigation.Region` --- .../Navigation/Regions/AllActiveRegion.cs | 27 -------- .../Regions/ContentControlRegionAdapter.cs | 64 ------------------- .../Prism.Avalonia/Prism.Avalonia.csproj | 2 + .../Regions/ContentControlRegionAdapter.cs | 4 ++ .../PrismInitializationExtensions.cs | 1 - 5 files changed, 6 insertions(+), 92 deletions(-) delete mode 100644 src/Avalonia/Prism.Avalonia/Navigation/Regions/AllActiveRegion.cs delete mode 100644 src/Avalonia/Prism.Avalonia/Navigation/Regions/ContentControlRegionAdapter.cs diff --git a/src/Avalonia/Prism.Avalonia/Navigation/Regions/AllActiveRegion.cs b/src/Avalonia/Prism.Avalonia/Navigation/Regions/AllActiveRegion.cs deleted file mode 100644 index 4f43a6ade1..0000000000 --- a/src/Avalonia/Prism.Avalonia/Navigation/Regions/AllActiveRegion.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using Prism.Properties; - -namespace Prism.Navigation.Regions -{ - /// - /// Region that keeps all the views in it as active. Deactivation of views is not allowed. - /// - public class AllActiveRegion : Region - { - /// - /// Gets a readonly view of the collection of all the active views in the region. These are all the added views. - /// - /// An of all the active views. - public override IViewsCollection ActiveViews => Views; - - /// - /// Deactivate is not valid in this Region. This method will always throw . - /// - /// The view to deactivate. - /// Every time this method is called. - public override void Deactivate(object view) - { - throw new InvalidOperationException(Resources.DeactiveNotPossibleException); - } - } -} diff --git a/src/Avalonia/Prism.Avalonia/Navigation/Regions/ContentControlRegionAdapter.cs b/src/Avalonia/Prism.Avalonia/Navigation/Regions/ContentControlRegionAdapter.cs deleted file mode 100644 index f2a10cf66b..0000000000 --- a/src/Avalonia/Prism.Avalonia/Navigation/Regions/ContentControlRegionAdapter.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Avalonia.Controls; -using Prism.Properties; -using System; -using System.Collections.Specialized; -using System.Linq; - -namespace Prism.Navigation.Regions -{ - /// - /// Adapter that creates a new and monitors its - /// active view to set it on the adapted . - /// - public class ContentControlRegionAdapter : RegionAdapterBase - { - /// - /// Initializes a new instance of . - /// - /// The factory used to create the region behaviors to attach to the created regions. - public ContentControlRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) - : base(regionBehaviorFactory) - { - } - - /// - /// Adapts a to an . - /// - /// The new region being used. - /// The object to adapt. - protected override void Adapt(IRegion region, ContentControl regionTarget) - { - if (regionTarget == null) - throw new ArgumentNullException(nameof(regionTarget)); - - bool contentIsSet = regionTarget.Content != null; - contentIsSet = contentIsSet || regionTarget[ContentControl.ContentProperty] != null; - - if (contentIsSet) - throw new InvalidOperationException(Resources.ContentControlHasContentException); - - region.ActiveViews.CollectionChanged += delegate - { - regionTarget.Content = region.ActiveViews.FirstOrDefault(); - }; - - region.Views.CollectionChanged += - (sender, e) => - { - if (e.Action == NotifyCollectionChangedAction.Add && region.ActiveViews.Count() == 0) - { - region.Activate(e.NewItems[0]); - } - }; - } - - /// - /// Creates a new instance of . - /// - /// A new instance of . - protected override IRegion CreateRegion() - { - return new SingleActiveRegion(); - } - } -} diff --git a/src/Avalonia/Prism.Avalonia/Prism.Avalonia.csproj b/src/Avalonia/Prism.Avalonia/Prism.Avalonia.csproj index edf13bb8e6..0899553502 100644 --- a/src/Avalonia/Prism.Avalonia/Prism.Avalonia.csproj +++ b/src/Avalonia/Prism.Avalonia/Prism.Avalonia.csproj @@ -26,6 +26,8 @@ Prism.Avalonia helps you more easily design and build rich, flexible, and easy t + + diff --git a/src/Wpf/Prism.Wpf/Navigation/Regions/ContentControlRegionAdapter.cs b/src/Wpf/Prism.Wpf/Navigation/Regions/ContentControlRegionAdapter.cs index 52aa7a991c..702c4eb75c 100644 --- a/src/Wpf/Prism.Wpf/Navigation/Regions/ContentControlRegionAdapter.cs +++ b/src/Wpf/Prism.Wpf/Navigation/Regions/ContentControlRegionAdapter.cs @@ -29,7 +29,11 @@ protected override void Adapt(IRegion region, ContentControl regionTarget) throw new ArgumentNullException(nameof(regionTarget)); bool contentIsSet = regionTarget.Content != null; +#if AVALONIA + contentIsSet = contentIsSet || regionTarget[ContentControl.ContentProperty] != null; +#else contentIsSet = contentIsSet || regionTarget.HasBinding(ContentControl.ContentProperty); +#endif if (contentIsSet) throw new InvalidOperationException(Resources.ContentControlHasContentException); diff --git a/src/Wpf/Prism.Wpf/PrismInitializationExtensions.cs b/src/Wpf/Prism.Wpf/PrismInitializationExtensions.cs index 42a96e9ec6..e607e7dea0 100644 --- a/src/Wpf/Prism.Wpf/PrismInitializationExtensions.cs +++ b/src/Wpf/Prism.Wpf/PrismInitializationExtensions.cs @@ -44,7 +44,6 @@ internal static void RegisterRequiredTypes(this IContainerRegistry containerRegi internal static void RegisterDefaultRegionBehaviors(this IRegionBehaviorFactory regionBehaviors) { #if AVALONIA - //// Avalonia to WPF Equivilant: BindRegionContextToAvaloniaObjectBehavior == BindRegionContextToDependencyObjectBehavior regionBehaviors.AddIfMissing(BindRegionContextToAvaloniaObjectBehavior.BehaviorKey); #else regionBehaviors.AddIfMissing(BindRegionContextToDependencyObjectBehavior.BehaviorKey);