forked from dotnet/maui
/
AppHostBuilderExtensions.cs
151 lines (135 loc) · 6.84 KB
/
AppHostBuilderExtensions.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
#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Platform;
using Microsoft.Maui.Dispatching;
using System.Runtime.CompilerServices;
#if ANDROID
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Compatibility.Platform.Android;
using Microsoft.Maui.Controls.Compatibility.Platform.Android.AppCompat;
using FrameRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FastRenderers.FrameRenderer;
using LabelRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FastRenderers.LabelRenderer;
using ImageRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FastRenderers.ImageRenderer;
using ButtonRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FastRenderers.ButtonRenderer;
using DefaultRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.Platform.DefaultRenderer;
#elif WINDOWS
using Microsoft.Maui.Controls.Compatibility.Platform.UWP;
using BoxRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.BoxViewBorderRenderer;
using CellRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.TextCellRenderer;
using ResourcesProvider = Microsoft.Maui.Controls.Compatibility.Platform.UWP.WindowsResourcesProvider;
using StreamImagesourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.UWP.StreamImageSourceHandler;
using ImageLoaderSourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.UWP.UriImageSourceHandler;
using DefaultRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.DefaultRenderer;
#elif __IOS__
using Microsoft.Maui.Controls.Compatibility.Platform.iOS;
using WebViewRenderer = Microsoft.Maui.Controls.Compatibility.Platform.iOS.WkWebViewRenderer;
using RadioButtonRenderer = Microsoft.Maui.Controls.Compatibility.Platform.iOS.Platform.DefaultRenderer;
using DefaultRenderer = Microsoft.Maui.Controls.Compatibility.Platform.iOS.Platform.DefaultRenderer;
#elif TIZEN
using Microsoft.Maui.Controls.Compatibility.Platform.Tizen;
using Microsoft.Maui.Graphics.Skia;
using BoxRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Tizen.BoxViewRenderer;
using CollectionViewRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Tizen.StructuredItemsViewRenderer;
using OpenGLViewRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Tizen.DefaultRenderer;
using StreamImagesourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.Tizen.StreamImageSourceHandler;
using ImageLoaderSourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.Tizen.UriImageSourceHandler;
using DefaultRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Tizen.DefaultRenderer;
#endif
namespace Microsoft.Maui.Controls.Compatibility.Hosting
{
public static class MauiAppBuilderExtensions
{
internal const string UseMapperInstead = "This renderer is obsolete please try to use the corresponding handler/mapper";
internal static void CheckForCompatibility([CallerMemberName] string memberName = "")
{
Controls.Hosting.CompatibilityCheck.CheckForCompatibility(memberName);
}
internal static void ResetCompatibilityCheck()
{
Controls.Hosting.CompatibilityCheck.ResetCompatibilityCheck();
}
public static MauiAppBuilder UseMauiCompatibility(this MauiAppBuilder builder)
{
Controls.Hosting.CompatibilityCheck.UseCompatibility();
#if PLATFORM
// initialize compatibility DependencyService
DependencyService.SetToInitialized();
DependencyService.Register<NativeBindingService>();
DependencyService.Register<NativeValueConverterService>();
#endif
builder.ConfigureCompatibilityLifecycleEvents();
builder
.ConfigureMauiHandlers(handlers =>
{
#if PLATFORM
#if !WINDOWS
#if !(MACCATALYST || MACOS)
#pragma warning disable CS0612 // Type or member is obsolete
handlers.TryAddCompatibilityRenderer(typeof(OpenGLView), typeof(OpenGLViewRenderer));
#pragma warning restore CS0612 // Type or member is obsolete
#endif
#else
#pragma warning disable CS0618 // Type or member is obsolete
handlers.TryAddCompatibilityRenderer(typeof(Layout), typeof(LayoutRenderer));
#pragma warning restore CS0618 // Type or member is obsolete
#endif
handlers.TryAddCompatibilityRenderer(typeof(NativeViewWrapper), typeof(NativeViewWrapperRenderer));
#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CS0612 // Type or member is obsolete
handlers.TryAddCompatibilityRenderer(typeof(Microsoft.Maui.Controls.Compatibility.Layout<View>), typeof(DefaultRenderer));
handlers.TryAddCompatibilityRenderer(typeof(Microsoft.Maui.Controls.Compatibility.RelativeLayout), typeof(DefaultRenderer));
handlers.TryAddCompatibilityRenderer(typeof(Microsoft.Maui.Controls.Compatibility.AbsoluteLayout), typeof(DefaultRenderer));
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore CS0612 // Type or member is obsolete
// Shimmed renderers go directly to the registrar to load Image Handlers
Internals.Registrar.Registered.Register(typeof(FileImageSource), typeof(FileImageSourceHandler));
Internals.Registrar.Registered.Register(typeof(StreamImageSource), typeof(StreamImagesourceHandler));
Internals.Registrar.Registered.Register(typeof(UriImageSource), typeof(ImageLoaderSourceHandler));
#if !TIZEN
Internals.Registrar.Registered.Register(typeof(FontImageSource), typeof(FontImageSourceHandler));
#endif
Internals.Registrar.Registered.Register(typeof(Microsoft.Maui.EmbeddedFont), typeof(Microsoft.Maui.EmbeddedFontLoader));
#endif
#if IOS || MACCATALYST
Internals.Registrar.RegisterEffect("Xamarin", "ShadowEffect", typeof(ShadowEffect));
#endif
});
#if WINDOWS
builder.AddMauiCompat();
#endif
return builder;
}
static MauiAppBuilder AddMauiCompat(this MauiAppBuilder builder)
{
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IMauiInitializeService, MauiCompatInitializer>());
return builder;
}
class MauiCompatInitializer : IMauiInitializeService
{
public void Initialize(IServiceProvider services)
{
#if WINDOWS
var dispatcher =
services.GetService<IDispatcher>() ??
MauiWinUIApplication.Current.Services.GetRequiredService<IDispatcher>();
dispatcher.DispatchIfRequired(() =>
{
var dictionaries = UI.Xaml.Application.Current?.Resources?.MergedDictionaries;
if (UI.Xaml.Application.Current?.Resources != null && dictionaries != null)
{
// Microsoft.Maui.Controls.Compatibility
UI.Xaml.Application.Current.Resources.AddLibraryResources("MicrosoftMauiControlsCompatibilityIncluded", "ms-appx:///Microsoft.Maui.Controls.Compatibility/Windows/Resources.xbf");
}
});
#endif
}
}
}
}