-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMauiProgram.cs
More file actions
75 lines (65 loc) · 2.82 KB
/
MauiProgram.cs
File metadata and controls
75 lines (65 loc) · 2.82 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
72
73
74
75
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.LifecycleEvents;
namespace InfiniteMeasureLooping;
public static class LoopDetector
{
public static readonly BindableProperty CurrentCountAttachedProperty = BindableProperty.CreateAttached("CurrentSizeChangedCount", typeof(int), typeof(LoopDetector), 0);
public static int GetCurrentCount(BindableObject bindable) => (int)bindable.GetValue(CurrentCountAttachedProperty);
public static void SetCurrentCount(BindableObject bindable, int value) => bindable.SetValue(CurrentCountAttachedProperty, value);
public static void Increment(BindableObject bindable) => SetCurrentCount(bindable, GetCurrentCount(bindable) + 1);
public static int IncrementLoopCount(this View view)
{
Increment(view);
return GetCurrentCount(view);
}
}
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App, MainWindow, RootNavigationPage>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-SemiBold.ttf", "OpenSansSemiBold");
});
var showPopup = true;
ViewHandler.ViewMapper.AppendToMapping("TheLoopDetector", (handler, _) =>
{
if (handler.VirtualView is View view)
{
view.SizeChanged += ViewOnSizeChanged;
view.Unloaded += ViewOnUnloaded;
void ViewOnUnloaded(object? sender, EventArgs e)
{
view.SizeChanged -= ViewOnSizeChanged;
view.Unloaded -= ViewOnUnloaded;
}
async void ViewOnSizeChanged(object? sender, EventArgs e)
{
var incrementLoopCount = view.IncrementLoopCount();
if (incrementLoopCount <= 1)
return;
if (incrementLoopCount > 500)
{
view.HeightRequest = 100;
if (showPopup)
{
showPopup = false;
await Application.Current?.Windows[0].Page?.DisplayAlertAsync("Loop Detected", "Current loop counter exceeded limit of 500 size change events setting HeightRequest manually - breaking ui-sizes", "OK");
showPopup = true;
}
}
Debug.WriteLine($"{view.GetType().Name} SizeChanged {incrementLoopCount} times", "TheLoopDetector");
}
}
});
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}