Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating .NET MAUI to use MauiApp/MauiAppBuilder pattern and use MS.Extensions.DependencyInjection #2137

Merged
merged 9 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions eng/Microsoft.Extensions.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,7 @@
<_UseNet6Packages Condition=" '$(TargetFramework.Contains(net6.0))' == 'true' or '$(PackageType)' == 'DotnetPlatform' ">true</_UseNet6Packages>
</PropertyGroup>
<PropertyGroup Condition=" '$(_UseNet6Packages)' != 'true' ">
<_MicrosoftHostingVersion>5.0.0</_MicrosoftHostingVersion>
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
<SystemCodeDomPackageVersion>4.7.0</SystemCodeDomPackageVersion>
<MicrosoftExtensionsPrimitivesPackageVersion>(_MicrosoftHostingVersion)</MicrosoftExtensionsPrimitivesPackageVersion>
<MicrosoftExtensionsHostingAbstractionsPackageVersion>$(_MicrosoftHostingVersion)</MicrosoftExtensionsHostingAbstractionsPackageVersion>
<MicrosoftExtensionsDependencyInjectionAbstractionsPackageVersion>$(_MicrosoftHostingVersion)</MicrosoftExtensionsDependencyInjectionAbstractionsPackageVersion>
<MicrosoftExtensionsDependencyInjectionPackageVersion>5.0.1</MicrosoftExtensionsDependencyInjectionPackageVersion>
<MicrosoftExtensionsConfigurationAbstractionsPackageVersion>$(_MicrosoftHostingVersion)</MicrosoftExtensionsConfigurationAbstractionsPackageVersion>
<MicrosoftExtensionsConfigurationPackageVersion>$(_MicrosoftHostingVersion)</MicrosoftExtensionsConfigurationPackageVersion>
<MicrosoftExtensionsLoggingAbstractionsPackageVersion>$(_MicrosoftHostingVersion)</MicrosoftExtensionsLoggingAbstractionsPackageVersion>
<MicrosoftExtensionsLoggingPackageVersion>$(_MicrosoftHostingVersion)</MicrosoftExtensionsLoggingPackageVersion>
<MicrosoftExtensionsLoggingConsolePackageVersion>$(_MicrosoftHostingVersion)</MicrosoftExtensionsLoggingConsolePackageVersion>
<MicrosoftExtensionsLoggingDebugPackageVersion>$(_MicrosoftHostingVersion)</MicrosoftExtensionsLoggingDebugPackageVersion>
<MicrosoftExtensionsFileProvidersAbstractionsPackageVersion>$(_MicrosoftHostingVersion)</MicrosoftExtensionsFileProvidersAbstractionsPackageVersion>
</PropertyGroup>
<!-- Microsoft.Maui.Graphics related Packages -->
<PropertyGroup>
Expand Down Expand Up @@ -69,6 +57,10 @@
Update="Microsoft.Extensions.FileProviders.Abstractions"
Version="$(MicrosoftExtensionsFileProvidersAbstractionsPackageVersion)"
/>
<PackageReference
Update="Microsoft.Extensions.Hosting"
Version="$(MicrosoftExtensionsHostingPackageVersion)"
/>
<PackageReference
Update="Microsoft.Extensions.Hosting.Abstractions"
Version="$(MicrosoftExtensionsHostingAbstractionsPackageVersion)"
Expand Down
4 changes: 4 additions & 0 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>14b34eb02bc8969b77c0d3a1e39fb38f450625cf</Sha>
</Dependency>
<Dependency Name="Microsoft.Extensions.Hosting" Version="6.0.0-rc.1.21417.1" CoherentParentDependency="Microsoft.AspNetCore.App.Runtime.win-x64">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>58efa4b79751a2dad08d9bf7ca67930f8160afe3</Sha>
</Dependency>
<Dependency Name="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0-rc.1.21417.1" CoherentParentDependency="Microsoft.AspNetCore.App.Runtime.win-x64">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>14b34eb02bc8969b77c0d3a1e39fb38f450625cf</Sha>
Expand Down
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<MicrosoftmacOSSdkPackageVersion>12.0.100-rc.1.521</MicrosoftmacOSSdkPackageVersion>
<MicrosoftExtensionsPrimitivesPackageVersion>6.0.0-rc.1.21417.1</MicrosoftExtensionsPrimitivesPackageVersion>
<MicrosoftExtensionsHostingAbstractionsPackageVersion>6.0.0-rc.1.21417.1</MicrosoftExtensionsHostingAbstractionsPackageVersion>
<MicrosoftExtensionsHostingPackageVersion>6.0.0-rc.1.21417.1</MicrosoftExtensionsHostingPackageVersion>
<MicrosoftExtensionsDependencyInjectionAbstractionsPackageVersion>6.0.0-rc.1.21417.1</MicrosoftExtensionsDependencyInjectionAbstractionsPackageVersion>
<MicrosoftExtensionsDependencyInjectionPackageVersion>6.0.0-rc.1.21417.1</MicrosoftExtensionsDependencyInjectionPackageVersion>
<MicrosoftExtensionsConfigurationAbstractionsPackageVersion>6.0.0-rc.1.21417.1</MicrosoftExtensionsConfigurationAbstractionsPackageVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;

namespace Microsoft.AspNetCore.Components.WebView.Maui
{
public static class BlazorWebViewRegistrationExtensions
{
public static TAppHostBuilder RegisterBlazorMauiWebView<TAppHostBuilder>(this TAppHostBuilder appHostBuilder) where TAppHostBuilder : IAppHostBuilder
public static MauiAppBuilder RegisterBlazorMauiWebView(this MauiAppBuilder appHostBuilder)
{
if (appHostBuilder is null)
{
throw new ArgumentNullException(nameof(appHostBuilder));
}

appHostBuilder.ConfigureMauiHandlers((_, handlers) => handlers.AddHandler<IBlazorWebView, BlazorWebViewHandler>());
appHostBuilder.ConfigureMauiHandlers(handlers => handlers.AddHandler<IBlazorWebView, BlazorWebViewHandler>());

return appHostBuilder;
}
Expand Down
21 changes: 14 additions & 7 deletions src/Compatibility/ControlGallery/src/Core/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@

namespace Microsoft.Maui.Controls.Compatibility.ControlGallery
{
public class Startup : IStartup
public static class MauiProgram
{
internal static bool UseBlazor = false;

public virtual void Configure(IAppHostBuilder appBuilder)
public static MauiApp CreateMauiApp()
{
appBuilder
var builder = CreateMauiAppBuilder();
return builder.Build();
}

public static MauiAppBuilder CreateMauiAppBuilder()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureMauiHandlers(handlers =>
{
Expand All @@ -26,14 +33,14 @@ public virtual void Configure(IAppHostBuilder appBuilder)
{
fonts.AddCompatibilityFonts(Device.GetAssemblies());
})
.ConfigureServices(services =>
{
DependencyService.Register(Device.GetAssemblies());
})
.ConfigureEffects(effects =>
{
effects.AddCompatibilityEffects(Device.GetAssemblies());
});

DependencyService.Register(Device.GetAssemblies());
Eilon marked this conversation as resolved.
Show resolved Hide resolved

return builder;
}
}
}
2 changes: 1 addition & 1 deletion src/Compatibility/ControlGallery/src/WinUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public App()
InitializeComponent();
}

protected override IStartup OnCreateStartup() => new WinUIStartup();
protected override MauiApp CreateMauiApp() => WinUIMauiProgram.CreateMauiApp();
}
}
11 changes: 7 additions & 4 deletions src/Compatibility/ControlGallery/src/WinUI/WinUIStartup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.Reflection.PortableExecutable;
using Microsoft.Maui.Controls.Compatibility.Platform.UWP;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Graphics;
Expand All @@ -15,13 +16,13 @@

namespace Microsoft.Maui.Controls.Compatibility.ControlGallery.WinUI
{
public class WinUIStartup : Startup
public class WinUIMauiProgram
{
public override void Configure(IAppHostBuilder appBuilder)
public static MauiApp CreateMauiApp()
{
base.Configure(appBuilder);
var builder = MauiProgram.CreateAppBuilder();

appBuilder.ConfigureLifecycleEvents(lifecycle => lifecycle
builder.ConfigureLifecycleEvents(lifecycle => lifecycle
.AddWindows(windows => windows
.OnLaunching((_, e) =>
{
Expand All @@ -32,6 +33,8 @@ public override void Configure(IAppHostBuilder appBuilder)
}
})
.OnActivated(WinUIPageStartup.OnActivated)));

return builder.Build();
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Compatibility/ControlGallery/src/iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CoreGraphics;
using Foundation;
using UIKit;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Compatibility.ControlGallery.iOS;
using Microsoft.Maui.Controls.Compatibility;
Expand Down Expand Up @@ -100,8 +101,10 @@ public string GetTestCloudDevice()
}

[Register("AppDelegate")]
public partial class AppDelegate : MauiUIApplicationDelegate<Startup>
public partial class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
UISwitch.Appearance.OnTintColor = UIColor.Red;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
internal static MauiAppBuilder ConfigureCompatibilityLifecycleEvents(this MauiAppBuilder builder) =>
builder.ConfigureLifecycleEvents(events => events.AddAndroid(OnConfigureLifeCycle));

static void OnConfigureLifeCycle(IAndroidLifecycleBuilder android)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
internal static MauiAppBuilder ConfigureCompatibilityLifecycleEvents(this MauiAppBuilder builder) =>
builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
internal static MauiAppBuilder ConfigureCompatibilityLifecycleEvents(this MauiAppBuilder builder) =>
builder.ConfigureLifecycleEvents(events => events.AddWindows(OnConfigureLifeCycle));

static void OnConfigureLifeCycle(IWindowsLifecycleBuilder windows)
Expand Down
59 changes: 26 additions & 33 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Shapes;
Expand Down Expand Up @@ -40,36 +41,25 @@

namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
public static class MauiAppBuilderExtensions
{
public static IAppHostBuilder UseMauiApp<TApp>(this IAppHostBuilder builder)
public static MauiAppBuilder UseMauiApp<TApp>(this MauiAppBuilder builder)
where TApp : class, IApplication
{
builder.ConfigureServices((context, collection) =>
{
collection.AddSingleton<IApplication, TApp>();
});

builder.Services.TryAddSingleton<IApplication, TApp>();
builder.SetupDefaults();

return builder;
}

public static IAppHostBuilder UseMauiApp<TApp>(this IAppHostBuilder builder, Func<IServiceProvider, TApp> implementationFactory)
public static MauiAppBuilder UseMauiApp<TApp>(this MauiAppBuilder builder, Func<IServiceProvider, TApp> implementationFactory)
where TApp : class, IApplication
{
builder.ConfigureServices((context, collection) =>
{
collection.AddSingleton<IApplication>(implementationFactory);
});

builder.Services.TryAddSingleton<IApplication>(implementationFactory);
builder.SetupDefaults();

return builder;
}


static IAppHostBuilder ConfigureImageSourceHandlers(this IAppHostBuilder builder)
static MauiAppBuilder ConfigureImageSourceHandlers(this MauiAppBuilder builder)
{
builder.ConfigureImageSources(services =>
{
Expand All @@ -82,7 +72,7 @@ static IAppHostBuilder ConfigureImageSourceHandlers(this IAppHostBuilder builder
return builder;
}

static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
static MauiAppBuilder SetupDefaults(this MauiAppBuilder builder)
{
builder.ConfigureCompatibilityLifecycleEvents();
builder.ConfigureImageSourceHandlers();
Expand Down Expand Up @@ -179,16 +169,30 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
// Update the mappings for IView/View to work specifically for Controls
VisualElement.RemapForControls();
Label.RemapForControls();
});

})
.ConfigureServices<MauiCompatBuilder>();
builder.AddMauiCompat();

return builder;
}

class MauiCompatBuilder : IMauiServiceBuilder
private static MauiAppBuilder AddMauiCompat(this MauiAppBuilder builder)
{
public void Configure(HostBuilderContext context, IServiceProvider services)
#if __IOS__ || MACCATALYST
builder.Services.TryAddSingleton<IGraphicsService>(NativeGraphicsService.Instance);
#elif __ANDROID__
builder.Services.TryAddSingleton<IGraphicsService>(NativeGraphicsService.Instance);
#elif WINDOWS
builder.Services.TryAddSingleton<IGraphicsService>(W2DGraphicsService.Instance);
#endif

builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IMauiInitializeService, MauiCompatInitializer>());
return builder;
}

class MauiCompatInitializer : IMauiInitializeService
{
public void Initialize(IServiceProvider services)
{
#if __ANDROID__ || __IOS__ || WINDOWS || MACCATALYST
CompatServiceProvider.SetServiceProvider(services);
Expand Down Expand Up @@ -216,17 +220,6 @@ public void Configure(HostBuilderContext context, IServiceProvider services)
#endif
}

public void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
#if __IOS__ || MACCATALYST
services.AddSingleton<IGraphicsService>(NativeGraphicsService.Instance);
#elif __ANDROID__
services.AddSingleton<IGraphicsService>(NativeGraphicsService.Instance);
#elif WINDOWS
services.AddSingleton<IGraphicsService>(W2DGraphicsService.Instance);
#endif
}

#if WINDOWS
static void AddLibraryResources(string key, string uri)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
internal static MauiAppBuilder ConfigureCompatibilityLifecycleEvents(this MauiAppBuilder builder) =>
builder.ConfigureLifecycleEvents(events => events.AddiOS(OnConfigureLifeCycle));

static void OnConfigureLifeCycle(IiOSLifecycleBuilder iOS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
namespace Maui.Controls.Sample.Droid
{
[Application]
public class MainApplication : MauiApplication<Startup>
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership) : base(handle, ownership)
{
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace Sample.MacCatalyst
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate<Startup>
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
namespace Maui.Controls.Sample.Profiling
{
[Application]
public class MainApplication : MauiApplication<Startup>
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership) : base(handle, ownership)
{
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
namespace Maui.Controls.Sample.Profiling
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate<Startup>
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
namespace Maui.Controls.Sample.Profiling
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate<Startup>
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
Loading