Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/Controls/samples/Controls.Sample.SingleProject/MyApp.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Application = Microsoft.Maui.Application;

namespace Maui.Controls.Sample.SingleProject
{
public class MyApp : MauiApp
public class MyApp : Application
{
public override IAppHostBuilder CreateBuilder() =>
base.CreateBuilder().ConfigureServices((ctx, services) =>
Expand Down
5 changes: 2 additions & 3 deletions src/Controls/samples/Controls.Sample/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Maui.Controls.Sample.Controls;
using Maui.Controls.Sample.Pages;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using MauiApplication = Microsoft.Maui.Application;

namespace Maui.Controls.Sample
{
public class MainWindow : Window
{
public MainWindow() : this(App.Current.Services.GetRequiredService<IPage>())
public MainWindow() : this(MauiApplication.Current.Services.GetRequiredService<IPage>())
{
}

Expand Down
28 changes: 25 additions & 3 deletions src/Controls/samples/Controls.Sample/MyApp.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Maui.Controls.Sample.Pages;
using Maui.Controls.Sample.Services;
using Maui.Controls.Sample.ViewModel;
Expand All @@ -10,10 +11,11 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Hosting;
using Application = Microsoft.Maui.Application;

namespace Maui.Controls.Sample
{
public class MyApp : MauiApp
public class MyApp : Application
{
public readonly static bool UseXamlPage = false;

Expand Down Expand Up @@ -45,15 +47,35 @@ public override IAppHostBuilder CreateBuilder() =>
fonts.AddFont("dokdo_regular.ttf", "Dokdo");
});

//IAppState state
// IAppState state
public override IWindow CreateWindow(IActivationState state)
{
Forms.Init(state);
return Services.GetRequiredService<IWindow>();
}

public override void OnCreated()
{
Debug.WriteLine("Application Created.");
}

public override void OnPaused()
{
Debug.WriteLine("Application Paused.");
}

public override void OnResumed()
{
Debug.WriteLine("Application Resumed.");
}

public override void OnStopped()
{
Debug.WriteLine("Application Stopped.");
}
}

//to use DI ServiceCollection and not the MAUI one
// To use DI ServiceCollection and not the MAUI one
public class DIExtensionsServiceProviderFactory : IServiceProviderFactory<ServiceCollection>
{
public ServiceCollection CreateBuilder(IServiceCollection services)
Expand Down
5 changes: 3 additions & 2 deletions src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using MauiApplication = Microsoft.Maui.Application;

namespace Maui.Controls.Sample.Pages
{
public class MainPage : ContentPage, IPage
{
MainPageViewModel _viewModel;
readonly MainPageViewModel _viewModel;

public MainPage() : this(App.Current.Services.GetService<MainPageViewModel>())
public MainPage() : this(MauiApplication.Current.Services.GetService<MainPageViewModel>())
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui;
using MauiApplication = Microsoft.Maui.Application;

namespace Maui.Controls.Sample.ViewModel
{
public class MainPageViewModel : ViewModelBase
{
private readonly IConfiguration Configuration;
readonly IConfiguration Configuration;
ITextService textService;

public MainPageViewModel() : this(new ITextService[] { App.Current.Services.GetService<ITextService>() })
public MainPageViewModel() : this(new ITextService[] { MauiApplication.Current.Services.GetService<ITextService>() })
{
}

public MainPageViewModel(IEnumerable<ITextService> textServices)
{
Configuration = App.Current.Services.GetService<IConfiguration>();
Configuration = MauiApplication.Current.Services.GetService<IConfiguration>();

//var logger = App.Current.Services.GetService<ILogger<MainPageViewModel>>();

Expand Down
60 changes: 45 additions & 15 deletions src/Core/src/App.cs → src/Core/src/Application.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Hosting;

namespace Microsoft.Maui
{
public abstract class App : IApp
public abstract class Application : IApplication
{
IServiceProvider? _serviceProvider;
IMauiContext? _context;

protected App()
protected Application()
{
if (Current != null)
throw new InvalidOperationException($"Only one {nameof(App)} instance is allowed");
throw new InvalidOperationException($"Only one {nameof(Application)} instance is allowed");

Current = this;
}

public static App? Current { get; internal set; }
public static Application? Current { get; internal set; }

public IServiceProvider? Services => _serviceProvider;

public IMauiContext? Context => _context;

//move to abstract
// Move to abstract
public virtual IAppHostBuilder CreateBuilder() => CreateDefaultBuilder();

internal void SetServiceProvider(IServiceProvider provider)
{
_serviceProvider = provider;
SetHandlerContext(provider.GetService<IMauiContext>());
}
public event EventHandler? Created;

internal void SetHandlerContext(IMauiContext? context)
{
_context = context;
}
public event EventHandler? Resumed;

public event EventHandler? Paused;

public event EventHandler? Stopped;

public static IAppHostBuilder CreateDefaultBuilder()
{
Expand All @@ -46,5 +43,38 @@ public static IAppHostBuilder CreateDefaultBuilder()

return builder;
}

public abstract IWindow CreateWindow(IActivationState state);

public virtual void OnCreated()
{
Created?.Invoke(this, EventArgs.Empty);
}

public virtual void OnResumed()
{
Resumed?.Invoke(this, EventArgs.Empty);
}

public virtual void OnPaused()
{
Paused?.Invoke(this, EventArgs.Empty);
}

public virtual void OnStopped()
{
Stopped?.Invoke(this, EventArgs.Empty);
}

internal void SetServiceProvider(IServiceProvider provider)
{
_serviceProvider = provider;
SetHandlerContext(provider.GetService<IMauiContext>());
}

internal void SetHandlerContext(IMauiContext? context)
{
_context = context;
}
}
}
15 changes: 15 additions & 0 deletions src/Core/src/Core/IApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Microsoft.Maui
{
/// <summary>
/// Encapsulates the Application and its available services.
/// </summary>
public interface IApplication : IApplicationLifetime
{
/// <summary>
/// Gets a collection of application-scoped services.
/// </summary>
IServiceProvider? Services { get; }
}
}
50 changes: 50 additions & 0 deletions src/Core/src/Core/IApplicationLifetime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;

namespace Microsoft.Maui
{
/// <summary>
/// Provides the methods and events to respond to Application lifecycle changes.
/// </summary>
public interface IApplicationLifetime
{
/// <summary>
/// Called when the application is first created.
/// </summary>
void OnCreated();

/// <summary>
/// Called when the application content will start interacting with the user.
/// </summary>
void OnResumed();

/// <summary>
/// Called when the application is not visible to the user.
/// </summary>
void OnPaused();

/// <summary>
/// Called before the application is closed.
/// </summary>
void OnStopped();

/// <summary>
/// This event is raised when the application is closed.
/// </summary>
event EventHandler? Created;

/// <summary>
/// This event is raised when the application is resumed.
/// </summary>
event EventHandler? Resumed;

/// <summary>
/// This event is raised when the application is pasued.
/// </summary>
event EventHandler? Paused;

/// <summary>
/// This event is raised when the application is closed.
/// </summary>
event EventHandler? Stopped;
}
}
2 changes: 1 addition & 1 deletion src/Core/src/Core/IWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Microsoft.Maui
/// <summary>
/// Provides the ability to create, configure, show, and manage Windows.
/// </summary>
public interface IWindow
public interface IWindow
{
/// <summary>
/// Gets or sets the .NET MAUI Context.
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Button/ButtonHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void MapTextColor(ButtonHandler handler, IButton button)

public static void MapFont(ButtonHandler handler, IButton button)
{
var services = App.Current?.Services
var services = Application.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();

Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Button/ButtonHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void MapPadding(ButtonHandler handler, IButton button)

public static void MapFont(ButtonHandler handler, IButton button)
{
var services = App.Current?.Services ??
var services = Application.Current?.Services ??
throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();

Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void MapPlaceholder(EntryHandler handler, IEntry entry)

public static void MapFont(EntryHandler handler, IEntry entry)
{
var services = App.Current?.Services
var services = Application.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();

Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Entry/EntryHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void OnTextChanged()

public static void MapFont(EntryHandler handler, IEntry entry)
{
var services = App.Current?.Services
var services = Application.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();

Expand Down
4 changes: 2 additions & 2 deletions src/Core/src/Handlers/Label/LabelHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public static void MapTextDecorations(LabelHandler handler, ILabel label)

public static void MapFont(LabelHandler handler, ILabel label)
{
var services = App.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var services = Application.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the Application.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(label, fontManager);
Expand Down
4 changes: 2 additions & 2 deletions src/Core/src/Handlers/Label/LabelHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static void MapTextDecorations(LabelHandler handler, ILabel label)

public static void MapFont(LabelHandler handler, ILabel label)
{
var services = App.Current?.Services ??
throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var services = Application.Current?.Services ??
throw new InvalidOperationException($"Unable to find service provider, the Application.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(label, fontManager);
Expand Down
10 changes: 4 additions & 6 deletions src/Core/src/Handlers/Layout/LayoutHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Android.Views;

namespace Microsoft.Maui.Handlers
Expand Down Expand Up @@ -29,24 +27,24 @@ public override void SetVirtualView(IView view)

_ = TypedNativeView ?? throw new InvalidOperationException($"{nameof(TypedNativeView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiApp.Current?.Context ?? throw new InvalidOperationException($"The MauiApp.Current.Context can't be null.");
_ = Application.Current?.Context ?? throw new InvalidOperationException($"The MauiApp.Current.Context can't be null.");

TypedNativeView.CrossPlatformMeasure = VirtualView.Measure;
TypedNativeView.CrossPlatformArrange = VirtualView.Arrange;

foreach (var child in VirtualView.Children)
{
TypedNativeView.AddView(child.ToNative(MauiApp.Current.Context));
TypedNativeView.AddView(child.ToNative(Application.Current.Context));
}
}

public void Add(IView child)
{
_ = TypedNativeView ?? throw new InvalidOperationException($"{nameof(TypedNativeView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiApp.Current?.Context ?? throw new InvalidOperationException($"The MauiApp.Current.Context can't be null.");
_ = Application.Current?.Context ?? throw new InvalidOperationException($"The MauiApp.Current.Context can't be null.");

TypedNativeView.AddView(child.ToNative(MauiApp.Current.Context!), 0);
TypedNativeView.AddView(child.ToNative(Application.Current.Context!), 0);
}

public void Remove(IView child)
Expand Down
Loading