-
Notifications
You must be signed in to change notification settings - Fork 428
/
Copy pathMauiProgram.cs
286 lines (250 loc) · 15.6 KB
/
MauiProgram.cs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using CommunityToolkit.Maui.ApplicationModel;
using CommunityToolkit.Maui.Maps;
using CommunityToolkit.Maui.Markup;
using CommunityToolkit.Maui.Media;
using CommunityToolkit.Maui.Sample.Models;
using CommunityToolkit.Maui.Sample.Pages;
using CommunityToolkit.Maui.Sample.Pages.Alerts;
using CommunityToolkit.Maui.Sample.Pages.Behaviors;
using CommunityToolkit.Maui.Sample.Pages.Converters;
using CommunityToolkit.Maui.Sample.Pages.Essentials;
using CommunityToolkit.Maui.Sample.Pages.Extensions;
using CommunityToolkit.Maui.Sample.Pages.ImageSources;
using CommunityToolkit.Maui.Sample.Pages.Layouts;
using CommunityToolkit.Maui.Sample.Pages.PlatformSpecific;
using CommunityToolkit.Maui.Sample.Pages.Views;
using CommunityToolkit.Maui.Sample.Resources.Fonts;
using CommunityToolkit.Maui.Sample.ViewModels;
using CommunityToolkit.Maui.Sample.ViewModels.Alerts;
using CommunityToolkit.Maui.Sample.ViewModels.Behaviors;
using CommunityToolkit.Maui.Sample.ViewModels.Converters;
using CommunityToolkit.Maui.Sample.ViewModels.Essentials;
using CommunityToolkit.Maui.Sample.ViewModels.Extensions;
using CommunityToolkit.Maui.Sample.ViewModels.ImageSources;
using CommunityToolkit.Maui.Sample.ViewModels.Layouts;
using CommunityToolkit.Maui.Sample.ViewModels.PlatformSpecific;
using CommunityToolkit.Maui.Sample.ViewModels.Views;
using CommunityToolkit.Maui.Sample.ViewModels.Views.AvatarView;
using CommunityToolkit.Maui.Sample.Views.Popups;
using CommunityToolkit.Maui.Storage;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Platform;
using Polly;
#if WINDOWS10_0_17763_0_OR_GREATER
using Microsoft.UI;
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml.Media;
#endif
[assembly: ExportFont("Poppins-Regular.ttf", Alias = "Poppins")]
[assembly: ExportFont("PlaywriteSK-Regular.ttf", Alias = "PlaywriteSK")]
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace CommunityToolkit.Maui.Sample;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder()
#if DEBUG
.UseMauiCommunityToolkit(options =>
{
options.SetShouldEnableSnackbarOnWindows(true);
})
#else
.UseMauiCommunityToolkit(options =>
{
options.SetShouldEnableSnackbarOnWindows(true);
options.SetShouldSuppressExceptionsInConverters(true);
options.SetShouldSuppressExceptionsInBehaviors(true);
options.SetShouldSuppressExceptionsInAnimations(true);
})
#endif
.UseMauiCommunityToolkitMarkup()
.UseMauiCommunityToolkitCamera()
.UseMauiCommunityToolkitMediaElement()
.UseMauiCommunityToolkitMaps("KEY") // You should add your own key here from bingmapsportal.com
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("Font Awesome 6 Brands-Regular-400.otf", FontFamilies.FontAwesomeBrands);
});
builder.ConfigureLifecycleEvents(events =>
{
#if WINDOWS10_0_17763_0_OR_GREATER
events.AddWindows(wndLifeCycleBuilder =>
{
wndLifeCycleBuilder.OnWindowCreated(window =>
{
window.SystemBackdrop = new MicaBackdrop { Kind = MicaKind.Base };
var titleBar = window.GetAppWindow()!.TitleBar;
titleBar.PreferredHeightOption = TitleBarHeightOption.Tall;
window.ExtendsContentIntoTitleBar = false;
IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
AppWindow winuiAppWindow = AppWindow.GetFromWindowId(win32WindowsId);
if (winuiAppWindow.Presenter is OverlappedPresenter p)
{
p.SetBorderAndTitleBar(true, true);
}
});
});
#endif
});
builder.Services.AddHttpClient<ByteArrayToImageSourceConverterViewModel>()
.AddStandardResilienceHandler(options => options.Retry = new MobileHttpRetryStrategyOptions());
builder.Services.AddSingleton<PopupSizeConstants>();
RegisterViewsAndViewModels(builder.Services);
RegisterEssentials(builder.Services);
#if DEBUG
builder.Logging.AddDebug().SetMinimumLevel(LogLevel.Trace);
#endif
return builder.Build();
}
static void RegisterViewsAndViewModels(in IServiceCollection services)
{
// Add Gallery Pages + ViewModels
services.AddTransient<AlertsGalleryPage, AlertsGalleryViewModel>();
services.AddTransient<BehaviorsGalleryPage, BehaviorsGalleryViewModel>();
services.AddTransient<ConvertersGalleryPage, ConvertersGalleryViewModel>();
services.AddTransient<EssentialsGalleryPage, EssentialsGalleryViewModel>();
services.AddTransient<ExtensionsGalleryPage, ExtensionsGalleryViewModel>();
services.AddTransient<ImageSourcesGalleryPage, ImageSourcesGalleryViewModel>();
services.AddTransient<LayoutsGalleryPage, LayoutsGalleryViewModel>();
services.AddTransient<ViewsGalleryPage, ViewsGalleryViewModel>();
services.AddTransient<PlatformSpecificGalleryPage, PlatformSpecificGalleryViewModel>();
// Add Alerts Pages + ViewModels
services.AddTransientWithShellRoute<SnackbarPage, SnackbarViewModel>();
services.AddTransientWithShellRoute<ToastPage, ToastViewModel>();
// Add AvatarView pages + ViewModels
services.AddTransientWithShellRoute<AvatarViewBindablePropertiesPage, AvatarViewBindablePropertiesViewModel>();
services.AddTransientWithShellRoute<AvatarViewBordersPage, AvatarViewBordersViewModel>();
services.AddTransientWithShellRoute<AvatarViewColorsPage, AvatarViewColorsViewModel>();
services.AddTransientWithShellRoute<AvatarViewDayOfWeekPage, AvatarViewDayOfWeekViewModel>();
services.AddTransientWithShellRoute<AvatarViewGesturesPage, AvatarViewGesturesViewModel>();
services.AddTransientWithShellRoute<AvatarViewImagesPage, AvatarViewImagesViewModel>();
services.AddTransientWithShellRoute<AvatarViewKeyboardPage, AvatarViewKeyboardViewModel>();
services.AddTransientWithShellRoute<AvatarViewRatingPage, AvatarViewRatingViewModel>();
services.AddTransientWithShellRoute<AvatarViewShadowsPage, AvatarViewShadowsViewModel>();
services.AddTransientWithShellRoute<AvatarViewShapesPage, AvatarViewShapesViewModel>();
services.AddTransientWithShellRoute<AvatarViewSizesPage, AvatarViewSizesViewModel>();
// Add Behaviors Pages + ViewModels
services.AddTransientWithShellRoute<AnimationBehaviorPage, AnimationBehaviorViewModel>();
services.AddTransientWithShellRoute<CharactersValidationBehaviorPage, CharactersValidationBehaviorViewModel>();
services.AddTransientWithShellRoute<EmailValidationBehaviorPage, EmailValidationBehaviorViewModel>();
services.AddTransientWithShellRoute<EventToCommandBehaviorPage, EventToCommandBehaviorViewModel>();
services.AddTransientWithShellRoute<IconTintColorBehaviorPage, IconTintColorBehaviorViewModel>();
services.AddTransientWithShellRoute<MaskedBehaviorPage, MaskedBehaviorViewModel>();
services.AddTransientWithShellRoute<MaxLengthReachedBehaviorPage, MaxLengthReachedBehaviorViewModel>();
services.AddTransientWithShellRoute<MultiValidationBehaviorPage, MultiValidationBehaviorViewModel>();
services.AddTransientWithShellRoute<NumericValidationBehaviorPage, NumericValidationBehaviorViewModel>();
services.AddTransientWithShellRoute<ProgressBarAnimationBehaviorPage, ProgressBarAnimationBehaviorViewModel>();
services.AddTransientWithShellRoute<RequiredStringValidationBehaviorPage, RequiredStringValidationBehaviorViewModel>();
services.AddTransientWithShellRoute<SelectAllTextBehaviorPage, SelectAllTextBehaviorViewModel>();
services.AddTransientWithShellRoute<SetFocusOnEntryCompletedBehaviorPage, SetFocusOnEntryCompletedBehaviorViewModel>();
services.AddTransientWithShellRoute<StatusBarBehaviorPage, StatusBarBehaviorViewModel>();
services.AddTransientWithShellRoute<TextValidationBehaviorPage, TextValidationBehaviorViewModel>();
services.AddTransientWithShellRoute<TouchBehaviorPage, TouchBehaviorViewModel>();
services.AddTransientWithShellRoute<UriValidationBehaviorPage, UriValidationBehaviorViewModel>();
services.AddTransientWithShellRoute<UserStoppedTypingBehaviorPage, UserStoppedTypingBehaviorViewModel>();
// Add Converters Pages + ViewModels
services.AddTransientWithShellRoute<BoolToObjectConverterPage, BoolToObjectConverterViewModel>();
services.AddTransientWithShellRoute<ByteArrayToImageSourceConverterPage, ByteArrayToImageSourceConverterViewModel>();
services.AddTransientWithShellRoute<ColorsConverterPage, ColorsConverterViewModel>();
services.AddTransientWithShellRoute<CompareConverterPage, CompareConverterViewModel>();
services.AddTransientWithShellRoute<DateTimeOffsetConverterPage, DateTimeOffsetConverterViewModel>();
services.AddTransientWithShellRoute<DoubleToIntConverterPage, DoubleToIntConverterViewModel>();
services.AddTransientWithShellRoute<EnumToBoolConverterPage, EnumToBoolConverterViewModel>();
services.AddTransientWithShellRoute<EnumToIntConverterPage, EnumToIntConverterViewModel>();
services.AddTransientWithShellRoute<ImageResourceConverterPage, ImageResourceConverterViewModel>();
services.AddTransientWithShellRoute<IndexToArrayItemConverterPage, IndexToArrayItemConverterViewModel>();
services.AddTransientWithShellRoute<IntToBoolConverterPage, IntToBoolConverterViewModel>();
services.AddTransientWithShellRoute<InvertedBoolConverterPage, InvertedBoolConverterViewModel>();
services.AddTransientWithShellRoute<IsEqualConverterPage, IsEqualConverterViewModel>();
services.AddTransientWithShellRoute<IsInRangeConverterPage, IsInRangeConverterViewModel>();
services.AddTransientWithShellRoute<IsListNotNullOrEmptyConverterPage, IsListNotNullOrEmptyConverterViewModel>();
services.AddTransientWithShellRoute<IsListNullOrEmptyConverterPage, IsListNullOrEmptyConverterViewModel>();
services.AddTransientWithShellRoute<IsNotEqualConverterPage, IsNotEqualConverterViewModel>();
services.AddTransientWithShellRoute<IsNotNullConverterPage, IsNotNullConverterViewModel>();
services.AddTransientWithShellRoute<IsNullConverterPage, IsNullConverterViewModel>();
services.AddTransientWithShellRoute<IsStringNotNullOrEmptyConverterPage, IsStringNotNullOrEmptyConverterViewModel>();
services.AddTransientWithShellRoute<IsStringNotNullOrWhiteSpaceConverterPage, IsStringNotNullOrWhiteSpaceConverterViewModel>();
services.AddTransientWithShellRoute<IsStringNullOrEmptyConverterPage, IsStringNullOrEmptyConverterViewModel>();
services.AddTransientWithShellRoute<IsStringNullOrWhiteSpaceConverterPage, IsStringNullOrWhiteSpaceConverterViewModel>();
services.AddTransientWithShellRoute<ItemTappedEventArgsConverterPage, ItemTappedEventArgsConverterViewModel>();
services.AddTransientWithShellRoute<ListToStringConverterPage, ListToStringConverterViewModel>();
services.AddTransientWithShellRoute<MathExpressionConverterPage, MathExpressionConverterViewModel>();
services.AddTransientWithShellRoute<MultiConverterPage, MultiConverterViewModel>();
services.AddTransientWithShellRoute<MultiMathExpressionConverterPage, MultiMathExpressionConverterViewModel>();
services.AddTransientWithShellRoute<SelectedItemEventArgsConverterPage, SelectedItemEventArgsConverterViewModel>();
services.AddTransientWithShellRoute<StateToBooleanConverterPage, StateToBooleanConverterViewModel>();
services.AddTransientWithShellRoute<StringToListConverterPage, StringToListConverterViewModel>();
services.AddTransientWithShellRoute<TextCaseConverterPage, TextCaseConverterViewModel>();
services.AddTransientWithShellRoute<VariableMultiValueConverterPage, VariableMultiValueConverterViewModel>();
// Add Essentials Pages + ViewModels
services.AddTransientWithShellRoute<AppThemePage, AppThemeViewModel>();
services.AddTransientWithShellRoute<BadgePage, BadgeViewModel>();
services.AddTransientWithShellRoute<FileSaverPage, FileSaverViewModel>();
services.AddTransientWithShellRoute<FolderPickerPage, FolderPickerViewModel>();
services.AddTransientWithShellRoute<SpeechToTextPage, SpeechToTextViewModel>();
// Add Extensions Pages + ViewModels
services.AddTransientWithShellRoute<ColorAnimationExtensionsPage, ColorAnimationExtensionsViewModel>();
services.AddTransientWithShellRoute<KeyboardExtensionsPage, KeyboardExtensionsViewModel>();
// Add ImageSources Pages + ViewModels
services.AddTransientWithShellRoute<GravatarImageSourcePage, GravatarImageSourceViewModel>();
// Add Layouts Pages + ViewModels
services.AddTransientWithShellRoute<DockLayoutPage, DockLayoutViewModel>();
services.AddTransientWithShellRoute<StateContainerPage, StateContainerViewModel>();
services.AddTransientWithShellRoute<UniformItemsLayoutPage, UniformItemsLayoutViewModel>();
// Add PlatformSpecific Pages + ViewModels
services.AddTransientWithShellRoute<NavigationBarPage, NavigationBarAndroidViewModel>();
// Add Views Pages + ViewModels
services.AddTransientWithShellRoute<CameraViewPage, CameraViewViewModel>();
services.AddTransientWithShellRoute<DrawingViewPage, DrawingViewViewModel>();
services.AddTransientWithShellRoute<ExpanderPage, ExpanderViewModel>();
services.AddTransientWithShellRoute<BasicMapsPage, BasicMapsViewModel>();
services.AddTransientWithShellRoute<MapsPinsPage, MapsPinsViewModel>();
services.AddTransientWithShellRoute<LazyViewPage, LazyViewViewModel>();
services.AddTransientWithShellRoute<MediaElementPage, MediaElementViewModel>();
services.AddTransientWithShellRoute<CustomSizeAndPositionPopupPage, CustomSizeAndPositionPopupViewModel>();
services.AddTransientWithShellRoute<MultiplePopupPage, MultiplePopupViewModel>();
services.AddTransientWithShellRoute<PopupAnchorPage, PopupAnchorViewModel>();
services.AddTransientWithShellRoute<PopupLayoutAlignmentPage, PopupLayoutAlignmentViewModel>();
services.AddTransientWithShellRoute<PopupPositionPage, PopupPositionViewModel>();
services.AddTransientWithShellRoute<SemanticOrderViewPage, SemanticOrderViewPageViewModel>();
services.AddTransientWithShellRoute<ShowPopupInOnAppearingPage, ShowPopupInOnAppearingPageViewModel>();
services.AddTransientWithShellRoute<StylePopupPage, StylePopupViewModel>();
services.AddTransientWithShellRoute<PopupSizingIssuesPage, PopupSizingIssuesViewModel>();
// Add Popups
services.AddTransientPopup<CsharpBindingPopup, CsharpBindingPopupViewModel>();
services.AddTransientPopup<UpdatingPopup, UpdatingPopupViewModel>();
services.AddTransientPopup<XamlBindingPopup, XamlBindingPopupViewModel>();
}
static void RegisterEssentials(in IServiceCollection services)
{
services.AddSingleton<IDeviceDisplay>(DeviceDisplay.Current);
services.AddSingleton<IDeviceInfo>(DeviceInfo.Current);
services.AddSingleton<IFileSaver>(FileSaver.Default);
services.AddSingleton<IFileSystem>(FileSystem.Current);
services.AddSingleton<IFolderPicker>(FolderPicker.Default);
services.AddSingleton<IBadge>(Badge.Default);
services.AddSingleton<ISpeechToText>(SpeechToText.Default);
services.AddSingleton<ITextToSpeech>(TextToSpeech.Default);
}
static IServiceCollection AddTransientWithShellRoute<TPage, TViewModel>(this IServiceCollection services) where TPage : BasePage<TViewModel>
where TViewModel : BaseViewModel
{
return services.AddTransientWithShellRoute<TPage, TViewModel>(AppShell.GetPageRoute<TViewModel>());
}
sealed class MobileHttpRetryStrategyOptions : HttpRetryStrategyOptions
{
public MobileHttpRetryStrategyOptions()
{
BackoffType = DelayBackoffType.Exponential;
MaxRetryAttempts = 3;
UseJitter = true;
Delay = TimeSpan.FromSeconds(2);
}
}
}