summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Xamarin.Forms.Core/DependencyService.cs18
-rw-r--r--Xamarin.Forms.Core/Registrar.cs43
2 files changed, 42 insertions, 19 deletions
diff --git a/Xamarin.Forms.Core/DependencyService.cs b/Xamarin.Forms.Core/DependencyService.cs
index d3c43998..2e0ae056 100644
--- a/Xamarin.Forms.Core/DependencyService.cs
+++ b/Xamarin.Forms.Core/DependencyService.cs
@@ -14,8 +14,7 @@ namespace Xamarin.Forms
public static T Get<T>(DependencyFetchTarget fetchTarget = DependencyFetchTarget.GlobalInstance) where T : class
{
- if (!s_initialized)
- Initialize();
+ Initialize();
Type targetType = typeof(T);
@@ -64,12 +63,27 @@ namespace Xamarin.Forms
static void Initialize()
{
+ if (s_initialized)
+ {
+ return;
+ }
+
Assembly[] assemblies = Device.GetAssemblies();
if (Registrar.ExtraAssemblies != null)
{
assemblies = assemblies.Union(Registrar.ExtraAssemblies).ToArray();
}
+ Initialize(assemblies);
+ }
+
+ internal static void Initialize(Assembly[] assemblies)
+ {
+ if (s_initialized)
+ {
+ return;
+ }
+
Type targetAttrType = typeof(DependencyAttribute);
// Don't use LINQ for performance reasons
diff --git a/Xamarin.Forms.Core/Registrar.cs b/Xamarin.Forms.Core/Registrar.cs
index 1224edf6..0b13a611 100644
--- a/Xamarin.Forms.Core/Registrar.cs
+++ b/Xamarin.Forms.Core/Registrar.cs
@@ -31,15 +31,18 @@ namespace Xamarin.Forms
internal Type GetHandlerType(Type viewType)
{
- Type type = LookupHandlerType(viewType);
- if (type != null)
+ Type type;
+ if(LookupHandlerType(viewType, out type))
return type;
// lazy load render-view association with RenderWithAttribute (as opposed to using ExportRenderer)
- // TODO: change Registrar to a LazyImmutableDictionary and pass this logic to ctor as a delegate.
var attribute = viewType.GetTypeInfo().GetCustomAttribute<RenderWithAttribute>();
if (attribute == null)
+ {
+ Register(viewType, null); // Cache this result so we don't have to do GetCustomAttribute again
return null;
+ }
+
type = attribute.Type;
if (type.Name.StartsWith("_"))
@@ -51,30 +54,33 @@ namespace Xamarin.Forms
if (type.Name.StartsWith("_"))
{
- //var attrs = type.GetTypeInfo ().GetCustomAttributes ().ToArray ();
+ Register(viewType, null); // Cache this result so we don't work through this chain again
return null;
}
}
- Register(viewType, type);
- return LookupHandlerType(viewType);
+ Register(viewType, type); // Register this so we don't have to look for the RenderWith Attibute again in the future
+
+ return type;
}
- Type LookupHandlerType(Type viewType)
+ bool LookupHandlerType(Type viewType, out Type handlerType)
{
Type type = viewType;
- while (true)
+ while (type != null)
{
if (_handlers.ContainsKey(type))
- return _handlers[type];
+ {
+ handlerType = _handlers[type];
+ return true;
+ }
type = type.GetTypeInfo().BaseType;
- if (type == null)
- break;
}
- return null;
+ handlerType = null;
+ return false;
}
}
@@ -126,15 +132,16 @@ namespace Xamarin.Forms
}
string resolutionName = assembly.FullName;
- var resolutionNameAttribute = (ResolutionGroupNameAttribute)assembly.GetCustomAttribute(typeof(ResolutionGroupNameAttribute));
- if (resolutionNameAttribute != null)
- {
- resolutionName = resolutionNameAttribute.ShortName;
- }
Attribute[] effectAttributes = assembly.GetCustomAttributes(typeof(ExportEffectAttribute)).ToArray();
if (effectAttributes.Length > 0)
{
+ var resolutionNameAttribute = (ResolutionGroupNameAttribute)assembly.GetCustomAttribute(typeof(ResolutionGroupNameAttribute));
+ if (resolutionNameAttribute != null)
+ {
+ resolutionName = resolutionNameAttribute.ShortName;
+ }
+
foreach (Attribute attribute in effectAttributes)
{
var effect = (ExportEffectAttribute)attribute;
@@ -142,6 +149,8 @@ namespace Xamarin.Forms
}
}
}
+
+ DependencyService.Initialize(assemblies);
}
}
} \ No newline at end of file