diff --git a/src/Forms/Prism.Forms.Regions/Regions/Region.cs b/src/Forms/Prism.Forms.Regions/Regions/Region.cs index 79fec8b92dc994c8c4a460dced0e8b4ec6c9a39c..9884703466590e28c2c9fe2bc825b34b32eac601 100644 --- a/src/Forms/Prism.Forms.Regions/Regions/Region.cs +++ b/src/Forms/Prism.Forms.Regions/Regions/Region.cs @@ -229,6 +229,15 @@ namespace Prism.Regions } } + /// + /// Add a View by name. + /// + /// + /// + /// Xamarin.Forms is in maintenance mode. This feature is not being supported. + /// + public IRegionManager Add(string viewName) => throw new NotImplementedException(); + /// Adds a new view to the region. /// /// Adds a new view to the region. diff --git a/src/Maui/Prism.Maui/Regions/Region.cs b/src/Maui/Prism.Maui/Regions/Region.cs index cee2644137185ee948e2205a23c3ab2500898378..983cd87afc9baad3db1c7d0a5d598c844d1604a4 100644 --- a/src/Maui/Prism.Maui/Regions/Region.cs +++ b/src/Maui/Prism.Maui/Regions/Region.cs @@ -265,7 +265,7 @@ public class Region : BindableBase, IRegion, ITargetAwareRegion { if (view is not VisualElement visualElement) { - throw new Exception("The view must inherit from VisualElement."); + throw new UpdateRegionsException("The view must inherit from VisualElement."); } if (ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null) diff --git a/src/Prism.Core/Regions/IRegion.cs b/src/Prism.Core/Regions/IRegion.cs index d8b13e7fc67e47d397ec298533829e9803c70baa..205a743279487fe4f62290d3fbebc4f5068a7b45 100644 --- a/src/Prism.Core/Regions/IRegion.cs +++ b/src/Prism.Core/Regions/IRegion.cs @@ -38,6 +38,14 @@ namespace Prism.Regions /// The comparison to use. Comparison SortComparison { get; set; } + ///Adds a new view to the region. + /// + /// Adds a new view to the region. + /// + /// The view to add. + /// The that is set on the view. It will be the current region manager when using this overload. + IRegionManager Add(string viewName); + ///Adds a new view to the region. /// /// Adds a new view to the region. diff --git a/src/Wpf/Prism.Wpf/Regions/Behaviors/RegionCreationException.Desktop.cs b/src/Wpf/Prism.Wpf/Regions/Behaviors/RegionCreationException.Desktop.cs deleted file mode 100644 index ca93f5d4f2d40ad98ddcdd25c7dbe8773f391d24..0000000000000000000000000000000000000000 --- a/src/Wpf/Prism.Wpf/Regions/Behaviors/RegionCreationException.Desktop.cs +++ /dev/null @@ -1,24 +0,0 @@ - - -using System; -using System.Runtime.Serialization; - -namespace Prism.Regions.Behaviors -{ - /// - /// - /// - [Serializable] - public partial class RegionCreationException - { - /// - /// Initializes a new instance of the class with serialized data. - /// - /// The that holds the serialized object data about the exception being thrown. - /// The that contains contextual information about the source or destination. - protected RegionCreationException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - } -} diff --git a/src/Wpf/Prism.Wpf/Regions/Behaviors/RegionCreationException.cs b/src/Wpf/Prism.Wpf/Regions/Behaviors/RegionCreationException.cs deleted file mode 100644 index 759b5e833f7c175ed5a3f0a8705332366fea4fc3..0000000000000000000000000000000000000000 --- a/src/Wpf/Prism.Wpf/Regions/Behaviors/RegionCreationException.cs +++ /dev/null @@ -1,40 +0,0 @@ - - -using System; - -namespace Prism.Regions.Behaviors -{ - /// - /// Represents errors that occured during region creation. - /// - public partial class RegionCreationException : Exception - { - /// - /// Initializes a new instance of the - /// - public RegionCreationException() - { - } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public RegionCreationException(string message) - : base(message) - { - } - - /// - /// Initializes a new instance of the class with a specified error message and a reference - /// to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference - /// (Nothing in Visual Basic) if no inner exception is specified. - public RegionCreationException(string message, Exception inner) - : base(message, inner) - { - } - } -} diff --git a/src/Wpf/Prism.Wpf/Regions/Region.cs b/src/Wpf/Prism.Wpf/Regions/Region.cs index ce843247500bc0e261c7c667c665836ff2f3b3aa..d033c219b090046144fc94d6d85affe83dcc8541 100644 --- a/src/Wpf/Prism.Wpf/Regions/Region.cs +++ b/src/Wpf/Prism.Wpf/Regions/Region.cs @@ -32,6 +32,7 @@ namespace Prism.Regions private object _context; private IRegionManager _regionManager; private IRegionNavigationService _regionNavigationService; + private IContainerProvider _container; private Comparison _sort; @@ -41,6 +42,7 @@ namespace Prism.Regions public Region() { Behaviors = new RegionBehaviorCollection(this); + _container = ContainerLocator.Container; _sort = DefaultSortComparison; } @@ -226,6 +228,18 @@ namespace Prism.Regions } } + /// Adds a new view to the region. + /// + /// Adds a new view to the region. + /// + /// The view to add. + /// The that is set on the view if it is a . It will be the current region manager when using this overload. + public IRegionManager Add(string viewName) + { + var view = _container.Resolve(viewName); + return Add(view, viewName, false); + } + /// Adds a new view to the region. /// /// Adds a new view to the region. @@ -234,7 +248,7 @@ namespace Prism.Regions /// The that is set on the view if it is a . It will be the current region manager when using this overload. public IRegionManager Add(object view) { - return this.Add(view, null, false); + return Add(view, null, false); } /// @@ -250,7 +264,7 @@ namespace Prism.Regions throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.StringCannotBeNullOrEmpty, "viewName")); } - return this.Add(view, viewName, false); + return Add(view, viewName, false); } /// @@ -262,8 +276,8 @@ namespace Prism.Regions /// The that is set on the view if it is a . public virtual IRegionManager Add(object view, string viewName, bool createRegionManagerScope) { - IRegionManager manager = createRegionManagerScope ? this.RegionManager.CreateRegionManager() : this.RegionManager; - this.InnerAdd(view, viewName, manager); + IRegionManager manager = createRegionManagerScope ? RegionManager.CreateRegionManager() : RegionManager; + InnerAdd(view, viewName, manager); return manager; } @@ -273,11 +287,11 @@ namespace Prism.Regions /// The view to remove. public virtual void Remove(object view) { - ItemMetadata itemMetadata = this.GetItemMetadataOrThrow(view); + ItemMetadata itemMetadata = GetItemMetadataOrThrow(view); ItemMetadataCollection.Remove(itemMetadata); - if (view is DependencyObject dependencyObject && Regions.RegionManager.GetRegionManager(dependencyObject) == this.RegionManager) + if (view is DependencyObject dependencyObject && Regions.RegionManager.GetRegionManager(dependencyObject) == RegionManager) { dependencyObject.ClearValue(Regions.RegionManager.RegionManagerProperty); } @@ -300,7 +314,7 @@ namespace Prism.Regions /// The view to activate. public virtual void Activate(object view) { - ItemMetadata itemMetadata = this.GetItemMetadataOrThrow(view); + ItemMetadata itemMetadata = GetItemMetadataOrThrow(view); if (!itemMetadata.IsActive) { @@ -314,7 +328,7 @@ namespace Prism.Regions /// The view to deactivate. public virtual void Deactivate(object view) { - ItemMetadata itemMetadata = this.GetItemMetadataOrThrow(view); + ItemMetadata itemMetadata = GetItemMetadataOrThrow(view); if (itemMetadata.IsActive) { @@ -334,7 +348,7 @@ namespace Prism.Regions throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.StringCannotBeNullOrEmpty, "viewName")); } - ItemMetadata metadata = this.ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName); + ItemMetadata metadata = ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName); if (metadata != null) { @@ -351,7 +365,7 @@ namespace Prism.Regions /// A callback to execute when the navigation request is completed. public void RequestNavigate(Uri target, Action navigationCallback) { - this.RequestNavigate(target, navigationCallback, null); + RequestNavigate(target, navigationCallback, null); } /// @@ -362,22 +376,22 @@ namespace Prism.Regions /// The navigation parameters specific to the navigation request. public void RequestNavigate(Uri target, Action navigationCallback, INavigationParameters navigationParameters) { - this.NavigationService.RequestNavigate(target, navigationCallback, navigationParameters); + NavigationService.RequestNavigate(target, navigationCallback, navigationParameters); } private void InnerAdd(object view, string viewName, IRegionManager scopedRegionManager) { - if (this.ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null) + if (ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null) { throw new InvalidOperationException(Resources.RegionViewExistsException); } - ItemMetadata itemMetadata = new ItemMetadata(view); + var itemMetadata = new ItemMetadata(view); if (!string.IsNullOrEmpty(viewName)) { - if (this.ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName) != null) + if (ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName) != null) { - throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, Resources.RegionViewNameExistsException, viewName)); + throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.RegionViewNameExistsException, viewName)); } itemMetadata.Name = viewName; } @@ -388,7 +402,7 @@ namespace Prism.Regions Regions.RegionManager.SetRegionManager(dependencyObject, scopedRegionManager); } - this.ItemMetadataCollection.Add(itemMetadata); + ItemMetadataCollection.Add(itemMetadata); } private ItemMetadata GetItemMetadataOrThrow(object view) @@ -396,7 +410,7 @@ namespace Prism.Regions if (view == null) throw new ArgumentNullException(nameof(view)); - ItemMetadata itemMetadata = this.ItemMetadataCollection.FirstOrDefault(x => x.Item == view); + ItemMetadata itemMetadata = ItemMetadataCollection.FirstOrDefault(x => x.Item == view); if (itemMetadata == null) throw new ArgumentException(Resources.ViewNotInRegionException, nameof(view)); diff --git a/tests/Forms/Prism.Forms.Regions.Tests/Mocks/MockPresentationRegion.cs b/tests/Forms/Prism.Forms.Regions.Tests/Mocks/MockPresentationRegion.cs index f9d068c32c8e2166a83f71b3591106f7122f860d..f325b0b6f3c196e4d1608b83fb65f4ba07073608 100644 --- a/tests/Forms/Prism.Forms.Regions.Tests/Mocks/MockPresentationRegion.cs +++ b/tests/Forms/Prism.Forms.Regions.Tests/Mocks/MockPresentationRegion.cs @@ -17,6 +17,9 @@ namespace Prism.Forms.Regions.Mocks { Behaviors = new MockRegionBehaviorCollection(); } + + public IRegionManager Add(string viewName) => throw new NotImplementedException(); + public IRegionManager Add(object view) { MockViews.Items.Add(view); diff --git a/tests/Forms/Prism.Forms.Regions.Tests/Mocks/MockRegion.cs b/tests/Forms/Prism.Forms.Regions.Tests/Mocks/MockRegion.cs index 4dd6d763f4a268eb73dd463a2287628f9158ca10..f59d205eb3a21834d93512582f6fae9a29ddbed0 100644 --- a/tests/Forms/Prism.Forms.Regions.Tests/Mocks/MockRegion.cs +++ b/tests/Forms/Prism.Forms.Regions.Tests/Mocks/MockRegion.cs @@ -33,6 +33,8 @@ namespace Prism.Forms.Regions.Mocks public string Name { get; set; } + public IRegionManager Add(string viewName) => throw new NotImplementedException(); + public IRegionManager Add(object view) { _views.Add(view); diff --git a/tests/Wpf/Prism.Wpf.Tests/Mocks/MockPresentationRegion.cs b/tests/Wpf/Prism.Wpf.Tests/Mocks/MockPresentationRegion.cs index b770ada7db0585cc27e091dc84458cd563168cdd..ab63a17d33b22b9eae0e5e8c16b1834536adf741 100644 --- a/tests/Wpf/Prism.Wpf.Tests/Mocks/MockPresentationRegion.cs +++ b/tests/Wpf/Prism.Wpf.Tests/Mocks/MockPresentationRegion.cs @@ -14,6 +14,9 @@ namespace Prism.Wpf.Tests.Mocks { Behaviors = new MockRegionBehaviorCollection(); } + + public IRegionManager Add(string viewName) => throw new NotImplementedException(); + public IRegionManager Add(object view) { MockViews.Items.Add(view); diff --git a/tests/Wpf/Prism.Wpf.Tests/Mocks/MockRegion.cs b/tests/Wpf/Prism.Wpf.Tests/Mocks/MockRegion.cs index 63a58cd7680260b31dad6b5fe09f74bf3a8f5d8d..c6aac83c1a8001b7da71236d45a6371a9de4aee9 100644 --- a/tests/Wpf/Prism.Wpf.Tests/Mocks/MockRegion.cs +++ b/tests/Wpf/Prism.Wpf.Tests/Mocks/MockRegion.cs @@ -36,6 +36,8 @@ namespace Prism.Wpf.Tests.Mocks public string Name { get; set; } + public IRegionManager Add(string viewName) => throw new NotImplementedException(); + public IRegionManager Add(object view) { this.views.Add(view);