コード例 #1
0
        /// <summary>Use the <see cref="IDependencyResolver"/> to create the command classes.</summary>
        /// <param name="appRunner">the <see cref="AppRunner"/> instance</param>
        /// <param name="dependencyResolver">the <see cref="IDependencyResolver"/> to use</param>
        /// <param name="runInScope">if provided, the scope will be created at the beginning of the run and disposed at the end</param>
        /// <param name="argumentModelResolveStrategy">
        /// the <see cref="ResolveStrategy"/> used to resolve <see cref="IArgumentModel"/>s.</param>
        /// <param name="commandClassResolveStrategy">
        /// the <see cref="ResolveStrategy"/> used to resolve command classes. </param>
        /// <param name="useLegacyInjectDependenciesAttribute">
        /// when true, resolve instances for properties marked with [InjectProperty].
        /// This feature is deprecated and may be removed with next major release.
        /// </param>
        public static AppRunner UseDependencyResolver(
            this AppRunner appRunner,
            IDependencyResolver dependencyResolver,
            Func <CommandContext, IDisposable> runInScope = null,
            ResolveStrategy argumentModelResolveStrategy  = ResolveStrategy.TryResolve,
            ResolveStrategy commandClassResolveStrategy   = ResolveStrategy.Resolve,
            bool useLegacyInjectDependenciesAttribute     = false)
        {
            DependencyResolverMiddleware.UseDependencyResolver(appRunner, dependencyResolver,
                                                               argumentModelResolveStrategy, commandClassResolveStrategy, useLegacyInjectDependenciesAttribute);

            if (runInScope != null)
            {
                appRunner.Configure(b =>
                {
                    b.UseMiddleware((context, next) =>
                    {
                        using (runInScope(context))
                        {
                            return(next(context));
                        }
                    },
                                    MiddlewareSteps.DependencyResolver.BeginScope.Stage,
                                    MiddlewareSteps.DependencyResolver.BeginScope.Order);
                });
            }

            return(appRunner);
        }
コード例 #2
0
        private bool ConditionalTryResolve(Type type, out object?item, ResolveStrategy resolveStrategy)
        {
            if (BackingResolver == null)
            {
                item = null;
                return(false);
            }

            if (resolveStrategy == ResolveStrategy.TryResolve)
            {
                return(BackingResolver.TryResolve(type, out item));
            }

            item = BackingResolver.Resolve(type);
            if (item != null)
            {
                return(true);
            }

            if (resolveStrategy == ResolveStrategy.ResolveOrThrow)
            {
                throw new ResolverReturnedNullException(type);
            }
            return(false);
        }
コード例 #3
0
        /// <summary>
        /// Wires a control to a property.
        /// This should be called in the Fragment's OnCreateView, with the newly inflated layout.
        /// </summary>
        /// <param name="fragment">The fragment.</param>
        /// <param name="inflatedView">The inflated view.</param>
        /// <param name="resolveMembers">The resolve members.</param>
        public static void WireUpControls(this Fragment fragment, View inflatedView, ResolveStrategy resolveMembers = ResolveStrategy.Implicit)
        {
            if (fragment is null)
            {
                throw new ArgumentNullException(nameof(fragment));
            }

            var members = fragment.GetWireUpMembers(resolveMembers);

            foreach (var member in members)
            {
                try
                {
                    // Find the android control with the same name from the view
                    var view = inflatedView.GetControl(fragment.GetType().Assembly, member.GetResourceName());

                    // Set the activity field's value to the view with that identifier
                    member.SetValue(fragment, view);
                }
                catch (Exception ex)
                {
                    throw new
                          MissingFieldException("Failed to wire up the Property " + member.Name + " to a View in your layout with a corresponding identifier", ex);
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Wires a control to a property.
        /// </summary>
        /// <param name="activity">The Activity.</param>
        /// <param name="resolveMembers">The resolve members.</param>
        public static void WireUpControls(this Activity activity, ResolveStrategy resolveMembers = ResolveStrategy.Implicit)
        {
            if (activity == null)
            {
                throw new ArgumentNullException(nameof(activity));
            }

            var members = activity.GetWireUpMembers(resolveMembers);

            foreach (var member in members)
            {
                try
                {
                    // Find the android control with the same name
                    var view = activity.GetControl(member.GetResourceName());

                    // Set the activity field's value to the view with that identifier
                    member.SetValue(activity, view);
                }
                catch (Exception ex)
                {
                    throw new
                          MissingFieldException("Failed to wire up the Property " + member.Name + " to a View in your layout with a corresponding identifier", ex);
                }
            }
        }
コード例 #5
0
 /// <summary>Use the provided SimpleInjector container to resolve command classes and <see cref="IArgumentModel"/>s</summary>
 /// <param name="appRunner">the <see cref="AppRunner"/></param>
 /// <param name="container">the SimpleInjector container to use</param>
 /// <param name="runInScope">if provided, the scope will be created at the beginning of the run and disposed at the end</param>
 /// <param name="argumentModelResolveStrategy">
 /// the <see cref="ResolveStrategy"/> used to resolve <see cref="IArgumentModel"/>s.
 /// </param>
 /// <param name="commandClassResolveStrategy">
 /// the <see cref="ResolveStrategy"/> used to resolve command classes.
 /// </param>
 public static AppRunner UseSimpleInjector(
     this AppRunner appRunner,
     Container container,
     Func <CommandContext, IDisposable>?runInScope = null,
     ResolveStrategy argumentModelResolveStrategy  = ResolveStrategy.TryResolve,
     ResolveStrategy commandClassResolveStrategy   = ResolveStrategy.Resolve)
 {
     return(appRunner.UseDependencyResolver(new SimpleInjectorResolver(container),
                                            runInScope,
                                            argumentModelResolveStrategy,
                                            commandClassResolveStrategy));
 }
コード例 #6
0
 /// <summary>Use the provided Microsoft service provider to resolve command classes and <see cref="IArgumentModel"/>s</summary>
 /// <param name="appRunner">the <see cref="AppRunner"/></param>
 /// <param name="serviceProvider">the <see cref="IServiceProvider"/> to use</param>
 /// <param name="runInScope">if provided, the scope will be created at the beginning of the run and disposed at the end</param>
 /// <param name="argumentModelResolveStrategy">
 /// the <see cref="ResolveStrategy"/> used to resolve <see cref="IArgumentModel"/>s.
 /// </param>
 /// <param name="commandClassResolveStrategy">
 /// the <see cref="ResolveStrategy"/> used to resolve command classes.
 /// </param>
 public static AppRunner UseMicrosoftDependencyInjection(
     this AppRunner appRunner,
     IServiceProvider serviceProvider,
     Func <CommandContext, IDisposable>?runInScope = null,
     ResolveStrategy argumentModelResolveStrategy  = ResolveStrategy.TryResolve,
     ResolveStrategy commandClassResolveStrategy   = ResolveStrategy.Resolve)
 {
     return(appRunner.UseDependencyResolver(new MicrosoftDependencyInjectionResolver(serviceProvider),
                                            runInScope,
                                            argumentModelResolveStrategy,
                                            commandClassResolveStrategy));
 }
コード例 #7
0
        public static View InflateAndWireUpControls(
            this Fragment fragment,
            LayoutInflater inflater,
            ViewGroup viewGroup,
            int layout,
            ResolveStrategy resolveMembers = ResolveStrategy.Implicit,
            bool attachToRoot = false)
        {
            var view = inflater.Inflate(layout, viewGroup, attachToRoot);

            ControlFetcherMixin.WireUpControls(fragment, view, resolveMembers);
            return(view);
        }
コード例 #8
0
 /// <summary>Use the provided Autofac container to resolve command classes and <see cref="IArgumentModel"/>s</summary>
 /// <param name="appRunner">the <see cref="AppRunner"/></param>
 /// <param name="container">the Autofac container to use</param>
 /// <param name="runInScope">if provided, the scope will be created at the beginning of the run and disposed at the end</param>
 /// <param name="argumentModelResolveStrategy">
 /// the <see cref="ResolveStrategy"/> used to resolve <see cref="IArgumentModel"/>s.
 /// </param>
 /// <param name="commandClassResolveStrategy">
 /// the <see cref="ResolveStrategy"/> used to resolve command classes.
 /// </param>
 /// <param name="useLegacyInjectDependenciesAttribute">
 /// when true, resolve instances for properties marked with [InjectProperty].
 /// This feature is deprecated and may be removed with next major release.
 /// </param>
 public static AppRunner UseAutofac(
     this AppRunner appRunner,
     IContainer container,
     Func <CommandContext, IDisposable> runInScope = null,
     ResolveStrategy argumentModelResolveStrategy  = ResolveStrategy.TryResolve,
     ResolveStrategy commandClassResolveStrategy   = ResolveStrategy.Resolve,
     bool useLegacyInjectDependenciesAttribute     = false)
 {
     return(appRunner.UseDependencyResolver(new AutofacResolver(container),
                                            runInScope,
                                            argumentModelResolveStrategy,
                                            commandClassResolveStrategy,
                                            useLegacyInjectDependenciesAttribute));
 }
コード例 #9
0
        /// <summary>
        ///
        /// </summary>
        public static void WireUpControls(this ILayoutViewHost This, ResolveStrategy resolveMembers = ResolveStrategy.Implicit)
        {
            var members = This.getWireUpMembers(resolveMembers);

            members.ToList().ForEach(m => {
                try {
                    // Find the android control with the same name
                    var view = This.View.getControlInternal(m.PropertyType, m.getResourceName());

                    // Set the activity field's value to the view with that identifier
                    m.SetValue(This, view);
                } catch (Exception ex) {
                    throw new MissingFieldException("Failed to wire up the Property "
                                                    + m.Name + " to a View in your layout with a corresponding identifier", ex);
                }
            });
        }
コード例 #10
0
        /// <summary>
        /// Wires a control to a property.
        /// </summary>
        /// <param name="layoutHost">The layout view host.</param>
        /// <param name="resolveMembers">The resolve members.</param>
        public static void WireUpControls(this ILayoutViewHost layoutHost, ResolveStrategy resolveMembers = ResolveStrategy.Implicit)
        {
            var members = layoutHost.GetWireUpMembers(resolveMembers).ToList();

            foreach (var member in members)
            {
                try
                {
                    var view = layoutHost.View.GetControl(layoutHost.GetType().Assembly, member.GetResourceName());
                    member.SetValue(layoutHost, view);
                }
                catch (Exception ex)
                {
                    throw new
                          MissingFieldException("Failed to wire up the Property " + member.Name + " to a View in your layout with a corresponding identifier", ex);
                }
            }
        }
コード例 #11
0
 internal static AppRunner UseDependencyResolver(AppRunner appRunner, IDependencyResolver dependencyResolver,
                                                 ResolveStrategy argumentModelResolveStrategy,
                                                 ResolveStrategy commandClassResolveStrategy,
                                                 bool useLegacyInjectDependenciesAttribute)
 {
     return(appRunner.Configure(c =>
     {
         c.DependencyResolver = dependencyResolver;
         c.Services.Add(new ResolverService
         {
             ArgumentModelResolveStrategy = argumentModelResolveStrategy,
             CommandClassResolveStrategy = commandClassResolveStrategy
         });
         if (useLegacyInjectDependenciesAttribute)
         {
             c.UseMiddleware(LegacyInjectPropertiesDependencies, MiddlewareStages.PostBindValuesPreInvoke);
         }
     }));
 }
コード例 #12
0
        static IEnumerable <PropertyInfo> getWireUpMembers(this object This, ResolveStrategy resolveStrategy)
        {
            var members = This.GetType().GetRuntimeProperties();

            switch (resolveStrategy)
            {
            default:
            case ResolveStrategy.Implicit:
                return(members.Where(m => m.PropertyType.IsSubclassOf(typeof(View)) ||
                                     m.GetCustomAttribute <WireUpResourceAttribute>(true) != null));

            case ResolveStrategy.ExplicitOptIn:
                return(members.Where(m => m.GetCustomAttribute <WireUpResourceAttribute>(true) != null));

            case ResolveStrategy.ExplicitOptOut:
                return(members.Where(m => typeof(View).IsAssignableFrom(m.PropertyType) &&
                                     m.GetCustomAttribute <IgnoreResourceAttribute>(true) == null));
            }
        }
コード例 #13
0
        /// <summary>
        /// Wires a control to a property.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="resolveMembers">The resolve members.</param>
        public static void WireUpControls(this View view, ResolveStrategy resolveMembers = ResolveStrategy.Implicit)
        {
            var members = view.GetWireUpMembers(resolveMembers);

            foreach (var member in members)
            {
                try
                {
                    // Find the android control with the same name
                    var currentView = view.GetControl(view.GetType().Assembly, member.GetResourceName());

                    // Set the activity field's value to the view with that identifier
                    member.SetValue(view, currentView);
                }
                catch (Exception ex)
                {
                    throw new
                          MissingFieldException("Failed to wire up the Property " + member.Name + " to a View in your layout with a corresponding identifier", ex);
                }
            }
        }
コード例 #14
0
 internal static AppRunner UseDependencyResolver(AppRunner appRunner,
                                                 IDependencyResolver dependencyResolver,
                                                 Func <CommandContext, IDisposable>?runInScope,
                                                 ResolveStrategy argumentModelResolveStrategy,
                                                 ResolveStrategy commandClassResolveStrategy)
 {
     return(appRunner.Configure(c =>
     {
         c.DependencyResolver = dependencyResolver;
         c.Services.Add(new ResolverService
         {
             ArgumentModelResolveStrategy = argumentModelResolveStrategy,
             CommandClassResolveStrategy = commandClassResolveStrategy
         });
         if (runInScope != null)
         {
             c.UseMiddleware(RunInScope, MiddlewareSteps.DependencyResolver.BeginScope);
             c.Services.Add(new Config(runInScope));
         }
     }));
 }
コード例 #15
0
        /// <summary>
        /// A helper method to automatically resolve properties in an <see cref="Android.Support.V4.App.Fragment"/> to their respective elements in the layout.
        /// This should be called in the Fragement's OnCreateView, with the newly inflated layout.
        /// </summary>
        /// <param name="this">The fragment.</param>
        /// <param name="inflatedView">The newly inflated <see cref="View"/> returned from <see cref="LayoutInflater.Inflate"/>.</param>
        /// <param name="resolveMembers">The strategy used to resolve properties that either subclass <see cref="View"/>, have a <see cref="WireUpResourceAttribute"/> or have a <see cref="IgnoreResourceAttribute"/>.</param>
        public static void WireUpControls(this Android.Support.V4.App.Fragment @this, View inflatedView, ResolveStrategy resolveMembers = ResolveStrategy.Implicit)
        {
            var members = @this.GetWireUpMembers(ResolveStrategy.Implicit);

            members.ToList().ForEach(m =>
            {
                try
                {
                    // Find the android control with the same name from the view
                    var view = inflatedView.GetControlInternal(m.PropertyType, m.GetResourceName());

                    // Set the activity field's value to the view with that identifier
                    m.SetValue(@this, view);
                }
                catch (Exception ex)
                {
                    throw new MissingFieldException(
                        "Failed to wire up the Property "
                        + m.Name + " to a View in your layout with a corresponding identifier", ex);
                }
            });
        }