diff --git a/src/Uno.UI/LinkerDefinition.Wasm.xml b/src/Uno.UI/LinkerDefinition.Wasm.xml
index ffa020223a8c..e297a003290c 100644
--- a/src/Uno.UI/LinkerDefinition.Wasm.xml
+++ b/src/Uno.UI/LinkerDefinition.Wasm.xml
@@ -15,7 +15,8 @@
-
+
+
diff --git a/src/Uno.UI/UI/Xaml/Application.wasm.cs b/src/Uno.UI/UI/Xaml/Application.wasm.cs
index ffcabc1c8692..60e13bdd885d 100644
--- a/src/Uno.UI/UI/Xaml/Application.wasm.cs
+++ b/src/Uno.UI/UI/Xaml/Application.wasm.cs
@@ -44,6 +44,8 @@ public Application()
Package.SetEntryAssembly(this.GetType().Assembly);
CoreDispatcher.Main.RunAsync(CoreDispatcherPriority.Normal, Initialize);
+
+ ObserveApplicationVisibility();
}
[Preserve]
@@ -53,6 +55,27 @@ public static int DispatchSystemThemeChange()
return 0;
}
+ [Preserve]
+ public static int DispatchVisibilityChange(bool isVisible)
+ {
+ var application = Windows.UI.Xaml.Application.Current;
+ var window = Windows.UI.Xaml.Window.Current;
+ if (isVisible)
+ {
+ application?.LeavingBackground?.Invoke(application, new LeavingBackgroundEventArgs());
+ window?.OnVisibilityChanged(true);
+ window?.OnActivated(CoreWindowActivationState.CodeActivated);
+ }
+ else
+ {
+ window?.OnActivated(CoreWindowActivationState.Deactivated);
+ window?.OnVisibilityChanged(false);
+ application?.EnteredBackground?.Invoke(application, new EnteredBackgroundEventArgs());
+ }
+
+ return 0;
+ }
+
static partial void StartPartial(ApplicationInitializationCallback callback)
{
_startInvoked = true;
@@ -74,7 +97,6 @@ partial void ObserveSystemThemeChanges()
WebAssemblyRuntime.InvokeJS("Windows.UI.Xaml.Application.observeSystemTheme()");
}
-
private void Initialize()
{
using (WritePhaseEventTrace(TraceProvider.LauchedStart, TraceProvider.LauchedStop))
@@ -124,6 +146,11 @@ partial void OnSuspendingPartial()
this.Log().LogWarning($"This platform does not support asynchronous Suspending deferral. Code executed after the of the method called by Suspending may not get executed.");
}
}
+
+ private void ObserveApplicationVisibility()
+ {
+ WebAssemblyRuntime.InvokeJS("Windows.UI.Xaml.Application.observeVisibility()");
+ }
}
}
#endif
diff --git a/src/Uno.UI/WasmScripts/Uno.UI.d.ts b/src/Uno.UI/WasmScripts/Uno.UI.d.ts
index 3bb156854659..a4af1088421d 100644
--- a/src/Uno.UI/WasmScripts/Uno.UI.d.ts
+++ b/src/Uno.UI/WasmScripts/Uno.UI.d.ts
@@ -1116,8 +1116,10 @@ declare namespace Windows.UI.ViewManagement {
declare namespace Windows.UI.Xaml {
class Application {
private static dispatchThemeChange;
+ private static dispatchVisibilityChange;
static getDefaultSystemTheme(): string;
static observeSystemTheme(): void;
+ static observeVisibility(): void;
}
}
declare namespace Windows.UI.Xaml {
diff --git a/src/Uno.UI/WasmScripts/Uno.UI.js b/src/Uno.UI/WasmScripts/Uno.UI.js
index 769162a9880e..cb03bc61e766 100644
--- a/src/Uno.UI/WasmScripts/Uno.UI.js
+++ b/src/Uno.UI/WasmScripts/Uno.UI.js
@@ -4037,6 +4037,14 @@ var Windows;
});
}
}
+ static observeVisibility() {
+ if (!this.dispatchVisibilityChange) {
+ this.dispatchVisibilityChange = Module.mono_bind_static_method("[Uno.UI] Windows.UI.Xaml.Application:DispatchVisibilityChange");
+ }
+ document.addEventListener("visibilitychange", () => {
+ Application.dispatchVisibilityChange(document.visibilityState == "visible");
+ });
+ }
}
Xaml.Application = Application;
})(Xaml = UI.Xaml || (UI.Xaml = {}));
diff --git a/src/Uno.UI/ts/Windows/UI/Xaml/Application.ts b/src/Uno.UI/ts/Windows/UI/Xaml/Application.ts
index 5aac729a188f..02423e30099f 100644
--- a/src/Uno.UI/ts/Windows/UI/Xaml/Application.ts
+++ b/src/Uno.UI/ts/Windows/UI/Xaml/Application.ts
@@ -2,6 +2,7 @@
export class Application {
private static dispatchThemeChange: () => number;
+ private static dispatchVisibilityChange: (isVisible: boolean) => number;
public static getDefaultSystemTheme(): string {
if (window.matchMedia) {
@@ -13,19 +14,28 @@
}
}
return null;
- }
+ }
public static observeSystemTheme() {
if (!this.dispatchThemeChange) {
this.dispatchThemeChange = (Module).mono_bind_static_method("[Uno.UI] Windows.UI.Xaml.Application:DispatchSystemThemeChange");
}
- if (window.matchMedia)
- {
+ if (window.matchMedia) {
window.matchMedia('(prefers-color-scheme: dark)').addEventListener("change", () => {
Application.dispatchThemeChange();
});
}
}
- }
+
+ public static observeVisibility() {
+ if (!this.dispatchVisibilityChange) {
+ this.dispatchVisibilityChange = (Module).mono_bind_static_method("[Uno.UI] Windows.UI.Xaml.Application:DispatchVisibilityChange");
+ }
+
+ document.addEventListener("visibilitychange", () => {
+ Application.dispatchVisibilityChange(document.visibilityState == "visible");
+ });
+ }
+ }
}
diff --git a/src/Uno.UWP/ApplicationModel/EnteredBackgroundEventArgs.cs b/src/Uno.UWP/ApplicationModel/EnteredBackgroundEventArgs.cs
index 9583149962c4..1d8179395821 100644
--- a/src/Uno.UWP/ApplicationModel/EnteredBackgroundEventArgs.cs
+++ b/src/Uno.UWP/ApplicationModel/EnteredBackgroundEventArgs.cs
@@ -2,5 +2,8 @@ namespace Windows.ApplicationModel
{
public partial class EnteredBackgroundEventArgs : IEnteredBackgroundEventArgs
{
+ internal EnteredBackgroundEventArgs()
+ {
+ }
}
}
diff --git a/src/Uno.UWP/ApplicationModel/LeavingBackgroundEventArgs.cs b/src/Uno.UWP/ApplicationModel/LeavingBackgroundEventArgs.cs
index 04639cbfa67d..5ea215f44a0d 100644
--- a/src/Uno.UWP/ApplicationModel/LeavingBackgroundEventArgs.cs
+++ b/src/Uno.UWP/ApplicationModel/LeavingBackgroundEventArgs.cs
@@ -2,5 +2,8 @@ namespace Windows.ApplicationModel
{
public partial class LeavingBackgroundEventArgs : ILeavingBackgroundEventArgs
{
+ internal LeavingBackgroundEventArgs()
+ {
+ }
}
}