diff --git a/src/Forms/Prism.Forms.Regions/Ioc/RegionResolverOverrides.cs b/src/Forms/Prism.Forms.Regions/Ioc/RegionResolverOverrides.cs index a019f619bc..9ed0a5e898 100644 --- a/src/Forms/Prism.Forms.Regions/Ioc/RegionResolverOverrides.cs +++ b/src/Forms/Prism.Forms.Regions/Ioc/RegionResolverOverrides.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using Prism.Navigation; using Prism.Navigation.Regions; -using Prism.Navigation.Regions; namespace Prism.Ioc { diff --git a/src/Forms/Prism.Forms.Regions/Navigation/Regions/Region.cs b/src/Forms/Prism.Forms.Regions/Navigation/Regions/Region.cs index 9ac413648f..8f1bcefbcf 100644 --- a/src/Forms/Prism.Forms.Regions/Navigation/Regions/Region.cs +++ b/src/Forms/Prism.Forms.Regions/Navigation/Regions/Region.cs @@ -282,12 +282,12 @@ private void InnerAdd(object view, string viewName, IRegionManager scopedRegionM { if (ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null) { - throw new InvalidOperationException(Resources.RegionViewExistsException); + throw new RegionViewException(Resources.RegionViewExistsException); } if (view is not VisualElement visualElement) { - throw new Exception("View is not a Visual Element"); + throw new RegionViewException(Resources.RegionViewIsNotVisualElementException); } var itemMetadata = new ItemMetadata(visualElement); @@ -295,7 +295,7 @@ private void InnerAdd(object view, string viewName, IRegionManager scopedRegionM { if (ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName) != null) { - throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.RegionViewNameExistsException, viewName)); + throw new RegionViewException(string.Format(CultureInfo.InvariantCulture, Resources.RegionViewNameExistsException, viewName)); } itemMetadata.Name = viewName; } diff --git a/src/Forms/Prism.Forms.Regions/Properties/Resources.Designer.cs b/src/Forms/Prism.Forms.Regions/Properties/Resources.Designer.cs index 6f3de9544b..d737b72762 100644 --- a/src/Forms/Prism.Forms.Regions/Properties/Resources.Designer.cs +++ b/src/Forms/Prism.Forms.Regions/Properties/Resources.Designer.cs @@ -19,7 +19,7 @@ namespace Prism.Properties { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { @@ -290,6 +290,15 @@ internal static string RegionViewExistsException { } } + /// + /// Looks up a localized string similar to View is not a Visual Element.. + /// + internal static string RegionViewIsNotVisualElementException { + get { + return ResourceManager.GetString("RegionViewIsNotVisualElementException", resourceCulture); + } + } + /// /// Looks up a localized string similar to View with name '{0}' already exists in the region.. /// diff --git a/src/Forms/Prism.Forms.Regions/Properties/Resources.resx b/src/Forms/Prism.Forms.Regions/Properties/Resources.resx index e421f89102..0ccd4ef14f 100644 --- a/src/Forms/Prism.Forms.Regions/Properties/Resources.resx +++ b/src/Forms/Prism.Forms.Regions/Properties/Resources.resx @@ -203,6 +203,9 @@ View already exists in region. + + View is not a Visual Element. + View with name '{0}' already exists in the region. diff --git a/src/Prism.Core/Navigation/Regions/RegionViewException.cs b/src/Prism.Core/Navigation/Regions/RegionViewException.cs new file mode 100644 index 0000000000..fa865dd03f --- /dev/null +++ b/src/Prism.Core/Navigation/Regions/RegionViewException.cs @@ -0,0 +1,39 @@ +using System; +using System.Runtime.Serialization; + +namespace Prism.Navigation.Regions; + +/// +/// An exception when there is an issue with a View being added to a Region +/// +public sealed class RegionViewException : RegionException +{ + /// + /// Initializes a new + /// + public RegionViewException() + { + } + + /// + /// Initializes a new + /// + /// The Exception Message. + public RegionViewException(string message) : base(message) + { + } + + /// + public RegionViewException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + + /// + /// Initializes a new + /// + /// The Exception Message. + /// The Inner . + public RegionViewException(string message, Exception innerException) : base(message, innerException) + { + } +} diff --git a/tests/Forms/Prism.Forms.Regions.Tests/Tests/RegionFixture.cs b/tests/Forms/Prism.Forms.Regions.Tests/Tests/RegionFixture.cs index 166e490a9e..911d2a5ea2 100644 --- a/tests/Forms/Prism.Forms.Regions.Tests/Tests/RegionFixture.cs +++ b/tests/Forms/Prism.Forms.Regions.Tests/Tests/RegionFixture.cs @@ -94,7 +94,7 @@ public void CanAddAndRetrieveNamedViewInstance() [Fact] public void AddingDuplicateNamedViewThrows() { - var ex = Assert.Throws(() => + var ex = Assert.Throws(() => { IRegion region = new Region(); diff --git a/tests/Forms/Prism.Forms.Regions.Tests/Tests/RegionNavigationServiceFixture.cs b/tests/Forms/Prism.Forms.Regions.Tests/Tests/RegionNavigationServiceFixture.cs index 476c5cf418..ee55108df0 100644 --- a/tests/Forms/Prism.Forms.Regions.Tests/Tests/RegionNavigationServiceFixture.cs +++ b/tests/Forms/Prism.Forms.Regions.Tests/Tests/RegionNavigationServiceFixture.cs @@ -10,6 +10,7 @@ using Xamarin.Forms; using Xunit; using Region = Prism.Navigation.Regions.Region; +using NuGet.Frameworks; namespace Prism.Forms.Regions.Tests { @@ -1004,7 +1005,10 @@ public void WhenNavigatingWithNullCallback_ThenThrows() var region = new Region(); var navigationUri = new Uri("/", UriKind.Relative); - IContainerExtension container = new Mock().Object; + var containerMock = new Mock(); + containerMock.Setup(x => x.Resolve(typeof(IActiveRegionHelper))) + .Returns(new RegionResolverOverrides()); + var container = containerMock.Object; var contentLoader = new Mock(container).Object; IRegionNavigationJournal journal = Mock.Of(); @@ -1013,11 +1017,11 @@ public void WhenNavigatingWithNullCallback_ThenThrows() Region = region }; - ExceptionAssert.Throws( - () => - { - target.RequestNavigate(navigationUri); - }); + Exception ex = null; + target.RequestNavigate(navigationUri, x => ex = x.Exception); + + Assert.NotNull(ex); + Assert.IsType(ex); } [Fact] @@ -1040,7 +1044,11 @@ public void WhenNavigatingWithNoRegionSet_ThenMarshallExceptionToCallback() [Fact] public void WhenNavigatingWithNullUri_ThenMarshallExceptionToCallback() { - IContainerExtension container = new Mock().Object; + + var containerMock = new Mock(); + containerMock.Setup(x => x.Resolve(typeof(IActiveRegionHelper))) + .Returns(new RegionResolverOverrides()); + var container = containerMock.Object; var contentLoader = new Mock(container).Object; IRegionNavigationJournal journal = Mock.Of(); @@ -1053,7 +1061,7 @@ public void WhenNavigatingWithNullUri_ThenMarshallExceptionToCallback() target.RequestNavigate("", nr => error = nr.Exception); Assert.NotNull(error); - Assert.IsType(error); + Assert.IsType(error); }