Browse Source

AppBuilderBase: Allow to specify app factory.

Kiminuo 5 years ago
parent
commit
cd180770fb

+ 17 - 0
src/Avalonia.Controls/AppBuilderBase.cs

@@ -88,6 +88,23 @@ namespace Avalonia.Controls
             };
         }
 
+        /// <summary>
+        /// Begin configuring an <see cref="Application"/>.
+        /// </summary>
+        /// <param name="appFactory">Factory function for <typeparamref name="TApp"/>.</param>
+        /// <typeparam name="TApp">The subclass of <see cref="Application"/> to configure.</typeparam>
+        /// <remarks><paramref name="appFactory"/> is useful for passing of dependencies to <typeparamref name="TApp"/>.</remarks>
+        /// <returns>An <typeparamref name="TAppBuilder"/> instance.</returns>
+        public static TAppBuilder Configure<TApp>(Func<TApp> appFactory)
+            where TApp : Application
+        {
+            return new TAppBuilder()
+            {
+                ApplicationType = typeof(TApp),
+                _appFactory = appFactory
+            };
+        }
+
         protected TAppBuilder Self => (TAppBuilder)this;
 
         public TAppBuilder AfterSetup(Action<TAppBuilder> callback)

+ 42 - 2
tests/Avalonia.Controls.UnitTests/AppBuilderTests.cs

@@ -1,4 +1,5 @@
-using Avalonia.Controls.UnitTests;
+using System;
+using Avalonia.Controls.UnitTests;
 using Avalonia.Platform;
 using Xunit;
 
@@ -18,6 +19,22 @@ namespace Avalonia.Controls.UnitTests
         {
         }
 
+        public class AppWithDependencies : Application
+        {
+            public AppWithDependencies()
+            {
+                throw new NotSupportedException();
+            }
+
+            public AppWithDependencies(object dependencyA, object dependencyB)
+            {
+                DependencyA = dependencyA;
+                DependencyB = dependencyB;
+            }
+            public object DependencyA { get; }
+            public object DependencyB { get; }
+        }
+
         public class DefaultModule
         {
             public static bool IsLoaded = false;
@@ -53,7 +70,30 @@ namespace Avalonia.Controls.UnitTests
                 IsLoaded = true;
             }
         }
-        
+
+        [Fact]
+        public void UseAppFactory()
+        {
+            using (AvaloniaLocator.EnterScope())
+            {
+                ResetModuleLoadStates();
+
+                Func<AppWithDependencies> appFactory = () => new AppWithDependencies(dependencyA: new object(), dependencyB: new object());
+
+                var builder = AppBuilder.Configure<AppWithDependencies>(appFactory)
+                    .UseWindowingSubsystem(() => { })
+                    .UseRenderingSubsystem(() => { })
+                    .UseAvaloniaModules()
+                    .SetupWithoutStarting();
+
+                AppWithDependencies app = (AppWithDependencies)builder.Instance;
+                Assert.NotNull(app.DependencyA);
+                Assert.NotNull(app.DependencyB);
+
+                Assert.True(DefaultModule.IsLoaded);
+            }
+        }
+
         [Fact]
         public void LoadsDefaultModule()
         {