private void AddChild(string name, NavigationParameters parameters)
        {
            var page = _container.Resolve <Page>(name);

            if (ViewModelLocator.GetAutowireViewModel(page) == null)
            {
                ViewModelLocator.SetAutowireViewModel(page, true);
            }

            (page as INavigatingAware)?.OnNavigatingTo(parameters);
            (page?.BindingContext as INavigatingAware)?.OnNavigatingTo(parameters);

            Children.Add(page);
        }
Пример #2
0
        internal static object GetInstance(ICodeBrixContainer container, Type type,
                                           bool isSingleton,
                                           NamedParameter[] namedParameters     = null,
                                           Dictionary <string, object> settings = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            bool addToSingletons = false;

            if (isSingleton)
            {
                lock (singletonLocker)
                {
                    if (singletons.ContainsKey(type))
                    {
                        return(singletons[type]);
                    }
                    else
                    {
                        addToSingletons = true;
                    }
                }
            }

            if (IsSimpleType(type))
            {
                return(Activator.CreateInstance(type));
            }

            //Type is a reference type, need to try to create an instance
            object instance         = null;
            string unregisteredType = null;

            namedParameters = namedParameters ?? new NamedParameter[] { };

            IEnumerable <ConstructorInfo> ctors = type.GetTypeInfo().DeclaredConstructors ?? new List <ConstructorInfo>();

            // ReSharper disable PossibleMultipleEnumeration

            // Will first try to find a compatible constructor *with* parameters
            foreach (ConstructorInfo ctor in ctors.Where(w => (w.GetParameters()?.Length ?? 0) > 0))
            {
                ParameterInfo[] ctorParams = ctor.GetParameters() ?? new ParameterInfo[] { };
                if (ctorParams.All(a => namedParameters.Any(n => n.Name.Equals(a.Name, StringComparison.CurrentCultureIgnoreCase)) ||
                                   container.IsRegistered(a.ParameterType) ||
                                   ((container as CodeBrixContainer)?.XamarinDependencyExists(a.ParameterType) ?? false)))
                {
                    var parameters = new List <object>();
                    foreach (ParameterInfo ctorParam in ctorParams)
                    {
                        parameters.Add(namedParameters.FirstOrDefault(f => f.Name.Equals(ctorParam.Name, StringComparison.CurrentCultureIgnoreCase))?.Value
                                       ?? container.Resolve(ctorParam.ParameterType, namedParameters));
                    }
                    instance = ctor.Invoke(parameters.ToArray());
                    break;
                }
                else
                {
                    unregisteredType = unregisteredType
                                       ?? ctorParams.FirstOrDefault(f => namedParameters.All(n => !n.Name.Equals(f.Name, StringComparison.CurrentCultureIgnoreCase)) &&
                                                                    (!container.IsRegistered(f.ParameterType)))?.ParameterType?.Name;
                }
            }

            //Next, will try to create our instance using the default constructor
            instance = instance ?? FindDefaultConstructor(ctors)?.Invoke(new object[] { });

            // ReSharper restore PossibleMultipleEnumeration

            if (instance == null)
            {
                throw new InvalidOperationException($"An instance of type '{type.Name}' could not be constructed"
                                                    + $"{(unregisteredType == null ? "" : $" - the constructor parameter type of '{unregisteredType}' appears to be unregistered")}.");
            }