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

Window lifecycle #1754

Merged
merged 6 commits into from
Jul 22, 2021
Merged
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
36 changes: 36 additions & 0 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;

namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
builder.ConfigureLifecycleEvents(events => events.AddAndroid(OnConfigureLifeCycle));

static void OnConfigureLifeCycle(IAndroidLifecycleBuilder android)
{
android
.OnApplicationCreating((app) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens in the Application's OnCreate - before
// any UI has appeared.
// This creates a dummy MauiContext that wraps the Application.

var services = MauiApplication.Current.Services;
var mauiContext = new MauiContext(services, app);
var state = new ActivationState(mauiContext);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the activity.

var state = new ActivationState(mauiContext);
Forms.Init(state);
});
}
}
}
12 changes: 12 additions & 0 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.Standard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
using System;

namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No changes here. Just moving platform specific code to its own file

{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
builder;
}
}
50 changes: 50 additions & 0 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.Windows.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#nullable enable
using Microsoft.Maui.Controls.Compatibility.Platform.UWP;
using Microsoft.Maui.Graphics.Win2D;
using BoxRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.BoxViewBorderRenderer;
using CellRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.TextCellRenderer;
using Deserializer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.WindowsSerializer;
using ResourcesProvider = Microsoft.Maui.Controls.Compatibility.Platform.UWP.WindowsResourcesProvider;
using StreamImagesourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.UWP.StreamImageSourceHandler;
using ImageLoaderSourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.UWP.UriImageSourceHandler;
using DefaultRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.DefaultRenderer;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Controls.Compatibility;
using System;
using Microsoft.Maui.Hosting;

namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No changes here. Just moving platform specific code to its own file

builder.ConfigureLifecycleEvents(events => events.AddWindows(OnConfigureLifeCycle));

static void OnConfigureLifeCycle(IWindowsLifecycleBuilder windows)
{
windows.OnLaunching((app, args) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.
// We need to call this so the Window and Root Page can new up successfully
// The dispatcher that's inside of Forms.Init needs to be setup before the initial
// window and root page start creating.
// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
// MauiContext inside Forms

var services = MauiWinUIApplication.Current.Services;
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext, args);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the application.

var state = new ActivationState(mauiContext);
Forms.Init(state);
});
}
}
}
76 changes: 3 additions & 73 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

namespace Microsoft.Maui.Controls.Hosting
{
public static class AppHostBuilderExtensions
public static partial class AppHostBuilderExtensions
{
public static IAppHostBuilder UseMauiApp<TApp>(this IAppHostBuilder builder)
where TApp : class, IApplication
Expand All @@ -67,81 +67,11 @@ public static IAppHostBuilder UseMauiApp<TApp>(this IAppHostBuilder builder, Fun

return builder;
}


static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
{
builder.ConfigureLifecycleEvents(events =>
{
#if __ANDROID__
events.AddAndroid(android => android
.OnApplicationCreating((app) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens in the Application's OnCreate - before
// any UI has appeared.
// This creates a dummy MauiContext that wraps the Application.

var services = MauiApplication.Current.Services;
var mauiContext = new MauiContext(services, app);
var state = new ActivationState(mauiContext);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the activity.

var state = new ActivationState(mauiContext);
Forms.Init(state);
}));
#elif __IOS__
events.AddiOS(iOS => iOS
.WillFinishLaunching((app, options) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.

var services = MauiUIApplicationDelegate.Current.Services;
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
return true;
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the application.

var state = new ActivationState(mauiContext);
Forms.Init(state);
}));
#elif WINDOWS
events.AddWindows(windows => windows
.OnLaunching((app, args) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.
// We need to call this so the Window and Root Page can new up successfully
// The dispatcher that's inside of Forms.Init needs to be setup before the initial
// window and root page start creating.
// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
// MauiContext inside Forms

var services = MauiWinUIApplication.Current.Services;
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext, args);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the application.

var state = new ActivationState(mauiContext);
Forms.Init(state);
}));
#endif
});

builder.ConfigureCompatibilityLifecycleEvents();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No changes here. Just moving platform specific code to its own file

builder
.ConfigureMauiHandlers(handlers =>
{
Expand Down
35 changes: 35 additions & 0 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.iOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Controls.Compatibility;

namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No changes here. Just moving platform specific code to its own file

builder.ConfigureLifecycleEvents(events => events.AddiOS(OnConfigureLifeCycle));

static void OnConfigureLifeCycle(IiOSLifecycleBuilder iOS)
{
iOS.WillFinishLaunching((app, options) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.

var services = MauiUIApplicationDelegate.Current.Services;
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
return true;
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the application.

var state = new ActivationState(mauiContext);
Forms.Init(state);
});
}
}
}
10 changes: 5 additions & 5 deletions src/Compatibility/Core/src/Windows/Forms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ static void SetupInit(
{
MainWindow = mainWindow;

if (mainWindow is WindowsBasePage windowsPage)
{
windowsPage.LoadApplication(windowsPage.CreateApplication());
windowsPage.Activate();
}
//if (mainWindow is WindowsBasePage windowsPage)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've commented out WindowsBasePage because it's not a type that we need for compatibility or MAUI

//{
// windowsPage.LoadApplication(windowsPage.CreateApplication());
// windowsPage.Activate();
//}
}

IsInitialized = true;
Expand Down
13 changes: 5 additions & 8 deletions src/Compatibility/Core/src/Windows/NavigationPageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ public class NavigationPageRenderer : IVisualElementRenderer, ITitleProvider, IT
WImageSource _titleIcon;
VisualElementTracker<Page, PageControl> _tracker;
EntranceThemeTransition _transition;
Platform _platform;
bool _parentsLookedUp = false;

Platform Platform => _platform ?? (_platform = Platform.Current);

public NavigationPage Element { get; private set; }

protected VisualElementTracker<Page, PageControl> Tracker
Expand Down Expand Up @@ -676,7 +673,7 @@ void UpdateBackButton()
_container.SetBackButtonTitle(Element);
}

async void UpdateTitleOnParents()
void UpdateTitleOnParents()
{
if (Element == null || _currentPage == null)
return;
Expand Down Expand Up @@ -710,10 +707,10 @@ async void UpdateTitleOnParents()

if (_showTitle || (render != null && render.ShowTitle))
{
if (Platform != null)
{
await Platform.UpdateToolbarItems();
}
//if (Platform != null)
//{
// await Platform.UpdateToolbarItems();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently none of this code is running so commenting this out doesn't break anything.

//}
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/Compatibility/Core/src/Windows/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ public static IVisualElementRenderer CreateRenderer(VisualElement element)
return renderer;
}

internal static Platform Current
{
get
{
var frame = UI.Xaml.Window.Current?.Content as Microsoft.UI.Xaml.Controls.Frame;
var wbp = frame?.Content as WindowsBasePage;
return wbp?.Platform;
}
}
//internal static Platform Current
//{
// get
// {
// var frame = UI.Xaml.Window.Current?.Content as Microsoft.UI.Xaml.Controls.Frame;
// var wbp = frame?.Content as WindowsBasePage;
// return wbp?.Platform;
// }
//}

internal Platform(Microsoft.UI.Xaml.Window page)
{
Expand Down
Loading