Skip to content

Commit

Permalink
Merge pull request unoplatform#17902 from Youssef1313/onlaunched
Browse files Browse the repository at this point in the history
fix: OnLaunched should be called synchronously with app initialization
  • Loading branch information
Youssef1313 authored Aug 13, 2024
2 parents 08bd307 + 683da6f commit cb5ac18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/SamplesApp/SamplesApp.Shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
#endif

#if HAS_UNO_WINUI || WINAPPSDK
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
using DispatcherQueuePriority = Microsoft.UI.Dispatching.DispatcherQueuePriority;
using LaunchActivatedEventArgs = Microsoft/* UWP don't rename */.UI.Xaml.LaunchActivatedEventArgs;
#else
using DispatcherQueue = Windows.System.DispatcherQueue;
using DispatcherQueuePriority = Windows.System.DispatcherQueuePriority;
using LaunchActivatedEventArgs = Windows.ApplicationModel.Activation.LaunchActivatedEventArgs;
#endif

Expand Down Expand Up @@ -62,6 +66,7 @@ sealed public partial class App : Application
private static Microsoft.UI.Xaml.Window? _mainWindow;
private bool _wasActivated;
private bool _isSuspended;
private bool _gotOnLaunched;

static App()
{
Expand Down Expand Up @@ -94,6 +99,11 @@ public App()
this.Suspending += OnSuspending;
this.Resuming += OnResuming;
#endif

DispatcherQueue.GetForCurrentThread().TryEnqueue(DispatcherQueuePriority.High, () =>
{
Assert.IsTrue(_gotOnLaunched);
});
}

internal static Microsoft.UI.Xaml.Window? MainWindow => _mainWindow;
Expand All @@ -109,6 +119,7 @@ public App()
#endif
override void OnLaunched(LaunchActivatedEventArgs e)
{
_gotOnLaunched = true;
EnsureMainWindow();

SetupAndroidEnvironment();
Expand Down
10 changes: 7 additions & 3 deletions src/Uno.UI/UI/Xaml/Application.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ public partial class Application : IApplicationEvents
{
private static bool _startInvoked;

[ThreadStatic]
private static Application _current;

partial void InitializePartial()
{
_current = this;
SetCurrentLanguage();

if (!_startInvoked)
{
throw new InvalidOperationException("The application must be started using Application.Start first, e.g. Microsoft.UI.Xaml.Application.Start(_ => new App());");
}

_ = CoreDispatcher.Main.RunAsync(CoreDispatcherPriority.Normal, Initialize);

CoreApplication.SetInvalidateRender(compositionTarget =>
{
Debug.Assert(compositionTarget is null or CompositionTarget);
Expand Down Expand Up @@ -81,9 +83,11 @@ static partial void StartPartial(ApplicationInitializationCallback callback)
);

callback(new ApplicationInitializationCallbackParams());

_current.InvokeOnLaunched();
}

private void Initialize()
private void InvokeOnLaunched()
{
using (WritePhaseEventTrace(TraceProvider.LauchedStart, TraceProvider.LauchedStop))
{
Expand Down

0 comments on commit cb5ac18

Please sign in to comment.