示例#1
0
 /// <summary>
 /// Configures the host services with an insatnce of <see cref="IXamarinHostingPlatform"/>
 /// </summary>
 /// <typeparam name ="TPlatform">The type used to register <see cref="IXamarinHostingPlatform"/></typeparam>
 /// <param name="builder">The <see cref="IXamarinHostBuilder"/> to configure.</param>
 /// <param name="platform">The instance of <see cref="IXamarinHostingPlatform"/> to register.</param>
 /// <returns>The <see cref="IXamarinHostBuilder"/>.</returns>
 public static IXamarinHostBuilder UsePlatform <TPlatform>(this IXamarinHostBuilder builder, TPlatform platform) where TPlatform : IXamarinHostingPlatform
 {
     return(builder.ConfigureServices((context, services) =>
     {
         services.AddSingleton <IXamarinHostingPlatform>(platform);
     }));
 }
示例#2
0
 /// <summary>
 /// Configures the host services with an insatnce of <see cref="IXamarinApplication"/>
 /// </summary>
 /// <typeparam name ="TApp">The type used to register <see cref="IXamarinApplication"/></typeparam>
 /// <param name="builder">The <see cref="IXamarinHostBuilder"/> to configure.</param>
 /// <param name="app">The instance of <see cref="IXamarinApplication"/> to register.</param>
 /// <returns>The <see cref="IXamarinHostBuilder"/>.</returns>
 public static IXamarinHostBuilder UseApplication <TApp>(this IXamarinHostBuilder builder, TApp app) where TApp : class
 {
     return(builder.ConfigureServices((context, services) =>
     {
         XamarinApplicationDelegate @delegate = () => app;
         services.AddSingleton(@delegate);
     }));
 }
示例#3
0
        public static IXamarinHostBuilder UseNavigationRoot <TNavigationRoot>(this IXamarinHostBuilder builder, TNavigationRoot root) where TNavigationRoot : class
        {
            return(builder.ConfigureServices((context, services) =>
            {
                if (!typeof(INavigation).IsAssignableFrom(typeof(TNavigationRoot)) || !typeof(Application).IsAssignableFrom(typeof(TNavigationRoot)))
                {
                    throw new InvalidOperationException($"Navigation root must be of type {nameof(INavigation)} or {nameof(Application)}");
                }

                services.AddSingleton <XamarinNavigationRootDelegate>(sp => () => root);
            }));
        }
示例#4
0
        /// <summary>
        /// Configures the host services with an insatnce of <see cref="IXamarinApplication"/>
        /// </summary>
        /// <typeparam name ="TApp">The type used to register <see cref="IXamarinApplication"/>. Must either be of type Application or IXamarinApplication</typeparam>
        /// <param name="builder">The <see cref="IXamarinHostBuilder"/> to configure.</param>
        /// <returns>The <see cref="IXamarinHostBuilder"/>.</returns>
        public static IXamarinHostBuilder UseApplication <TApp>(this IXamarinHostBuilder builder) where TApp : class
        {
            return(builder.ConfigureServices((context, services) =>
            {
                var factory = new XamarinApplicationDelegateFactory();

                services.AddSingleton(sp =>
                {
                    factory.Init(sp);
                    return factory.Create <TApp>();
                });
            }));
        }
示例#5
0
 /// <summary>
 /// Configures the host services with a Startup class
 /// </summary>
 /// <param name="builder">The <see cref="IXamarinHostBuilder"/> to configure.</param>
 /// <param name="startupType">The class type to configure services with</param>
 /// <returns>The <see cref="IXamarinHostBuilder"/>.</returns>
 public static IXamarinHostBuilder UseStartup(this IXamarinHostBuilder builder, Type startupType)
 {
     return(builder
            .ConfigureServices((context, services) =>
     {
         if (typeof(IXamarinStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
         {
             services.AddSingleton(typeof(IXamarinStartup), startupType);
         }
         else
         {
             services.AddSingleton(typeof(IXamarinStartup), sp =>
             {
                 var hostingEnvironment = sp.GetRequiredService <IXamarinHostEnvironment>();
                 return new XamarinStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName));
             });
         }
     }));
 }
示例#6
0
        /// <summary>
        /// Configures the host services with a Startup class
        /// </summary>
        /// <typeparam name ="TStartup">The type used to configure services</typeparam>
        /// <param name="hostBuilder">The <see cref="IXamarinHostBuilder"/> to configure.</param>
        /// <param name="startup">The instance of startup class to configure services with</param>
        /// <returns>The <see cref="IXamarinHostBuilder"/>.</returns>
        public static IXamarinHostBuilder UseStartup <TStartup>(this IXamarinHostBuilder hostBuilder, TStartup startup) where TStartup : class
        {
            var startupType = typeof(TStartup);

            return(hostBuilder
                   .ConfigureServices((context, services) =>
            {
                if (typeof(IXamarinStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
                {
                    services.AddSingleton((IXamarinStartup)startup);
                }
                else
                {
                    services.AddSingleton <IXamarinStartup>(sp =>
                    {
                        var hostingEnvironment = sp.GetRequiredService <IXamarinHostEnvironment>();
                        return new XamarinStartup(StartupLoader.LoadMethods(startup, hostingEnvironment.EnvironmentName));
                    });
                }
            }));
        }