-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
71 lines (62 loc) · 2.07 KB
/
App.xaml.cs
File metadata and controls
71 lines (62 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using WorkActivityTracker.Services;
namespace WorkActivityTracker;
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override Window CreateWindow(IActivationState? activationState)
{
var window = new Window(new MainPage())
{
Title = "Work Activity Tracker",
Width = 1200,
Height = 800
};
// Hooking all'evento di cambio handler per accedere alla finestra nativa WinUI
// e intercettare la chiusura prima che avvenga (per avvisare l'utente di modifiche non salvate)
window.HandlerChanged += OnWindowHandlerChanged;
return window;
}
private void OnWindowHandlerChanged(object? sender, EventArgs e)
{
#if WINDOWS
if (sender is Microsoft.Maui.Controls.Window mauiWindow)
{
if (mauiWindow.Handler?.PlatformView is Microsoft.UI.Xaml.Window nativeWin)
{
nativeWin.AppWindow.Closing += OnNativeWindowClosing;
}
}
#endif
}
#if WINDOWS
private async void OnNativeWindowClosing(
Microsoft.UI.Windowing.AppWindow sender,
Microsoft.UI.Windowing.AppWindowClosingEventArgs args)
{
var service = IPlatformApplication.Current?.Services.GetService<UnsavedChangesService>();
if (service?.HasUnsavedChanges == true)
{
// Cancella la chiusura per poter mostrare il dialogo
args.Cancel = true;
var page = Application.Current?.Windows.FirstOrDefault()?.Page;
if (page != null)
{
bool esci = await page.DisplayAlert(
"Modifiche non salvate",
"Ci sono modifiche non salvate nel form attività. Vuoi uscire senza salvare?",
"Sì, esci senza salvare",
"Rimani"
);
if (esci)
{
service.HasUnsavedChanges = false;
Application.Current?.Quit();
}
}
}
}
#endif
}