don't throw exception if registering same route factory to same route name (#6775)

This commit is contained in:
Shane Neuville 2019-07-03 17:22:23 -06:00 коммит произвёл Samantha Houts
Родитель da8d36df9a
Коммит 6572230dcc
2 изменённых файлов: 29 добавлений и 13 удалений

Просмотреть файл

@ -135,7 +135,15 @@ namespace Xamarin.Forms.Core.UnitTests
var route = "dogs";
Routing.RegisterRoute(route, typeof(ShellItem));
Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("dogs", typeof(ShellItem)));
Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("dogs", typeof(ContentPage)));
}
[Test]
public async Task SucceedWhenAddingDuplicateRouteOfSameType()
{
var route = "dogs";
Routing.RegisterRoute(route, typeof(ShellItem));
Routing.RegisterRoute(route, typeof(ShellItem));
}
[Test]

Просмотреть файл

@ -26,9 +26,7 @@ namespace Xamarin.Forms
{
return IsImplicit(GetRoute(source));
}
internal static bool CompareWithRegisteredRoutes(string compare) => s_routes.ContainsKey(compare);
internal static void Clear()
{
s_routes.Clear();
@ -114,7 +112,7 @@ namespace Xamarin.Forms
{
if (!String.IsNullOrWhiteSpace(route))
route = FormatRoute(route);
ValidateRoute(route);
ValidateRoute(route, factory);
s_routes[route] = factory;
}
@ -127,12 +125,7 @@ namespace Xamarin.Forms
public static void RegisterRoute(string route, Type type)
{
if(!String.IsNullOrWhiteSpace(route))
route = FormatRoute(route);
ValidateRoute(route);
s_routes[route] = new TypeRouteFactory(type);
RegisterRoute(route, new TypeRouteFactory(type));
}
public static void SetRoute(Element obj, string value)
@ -140,11 +133,13 @@ namespace Xamarin.Forms
obj.SetValue(RouteProperty, value);
}
static void ValidateRoute(string route)
static void ValidateRoute(string route, RouteFactory routeFactory)
{
if (string.IsNullOrWhiteSpace(route))
throw new ArgumentNullException(nameof(route), "Route cannot be an empty string");
routeFactory = routeFactory ?? throw new ArgumentNullException(nameof(routeFactory), "Route Factory cannot be null");
var uri = new Uri(route, UriKind.RelativeOrAbsolute);
var parts = uri.OriginalString.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
@ -154,7 +149,8 @@ namespace Xamarin.Forms
throw new ArgumentException($"Route contains invalid characters in \"{part}\"");
}
if (CompareWithRegisteredRoutes(route))
RouteFactory existingRegistration = null;
if(s_routes.TryGetValue(route, out existingRegistration) && !existingRegistration.Equals(routeFactory))
throw new ArgumentException($"Duplicated Route: \"{route}\"");
}
@ -171,6 +167,18 @@ namespace Xamarin.Forms
{
return (Element)Activator.CreateInstance(_type);
}
public override bool Equals(object obj)
{
if ((obj is TypeRouteFactory typeRouteFactory))
return typeRouteFactory._type == _type;
return false;
}
public override int GetHashCode()
{
return _type.GetHashCode();
}
}
}
}