Skip to content

Commit c25eebe

Browse files
authored
Register Microsoft.Maui Graphics Platforms (#1441)
1 parent aad583b commit c25eebe

File tree

1 file changed

+66
-58
lines changed

1 file changed

+66
-58
lines changed

src/Compatibility/Core/src/AppHostBuilderExtensions.cs

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,33 @@
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Extensions.Hosting;
55
using Microsoft.Maui.Controls.Compatibility;
6+
using Microsoft.Maui.Hosting;
7+
using Microsoft.Maui.Controls.Shapes;
8+
using Microsoft.Maui.LifecycleEvents;
9+
using Microsoft.Maui.Graphics;
610

711
#if __ANDROID__
812
using Microsoft.Maui.Controls.Compatibility.Platform.Android;
913
using Microsoft.Maui.Controls.Compatibility.Platform.Android.AppCompat;
14+
using Microsoft.Maui.Graphics.Native;
1015
using FrameRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FastRenderers.FrameRenderer;
1116
using LabelRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FastRenderers.LabelRenderer;
1217
using ImageRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FastRenderers.ImageRenderer;
1318
using ButtonRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FastRenderers.ButtonRenderer;
1419
using DefaultRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.AppCompat.Platform.DefaultRenderer;
1520
#elif WINDOWS
1621
using Microsoft.Maui.Controls.Compatibility.Platform.UWP;
22+
using Microsoft.Maui.Graphics.Win2D;
1723
using BoxRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.BoxViewBorderRenderer;
1824
using CellRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.TextCellRenderer;
1925
using Deserializer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.WindowsSerializer;
2026
using ResourcesProvider = Microsoft.Maui.Controls.Compatibility.Platform.UWP.WindowsResourcesProvider;
2127
using StreamImagesourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.UWP.StreamImageSourceHandler;
2228
using ImageLoaderSourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.UWP.UriImageSourceHandler;
2329
using DefaultRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.DefaultRenderer;
24-
2530
#elif __IOS__
2631
using Microsoft.Maui.Controls.Compatibility.Platform.iOS;
32+
using Microsoft.Maui.Graphics.Native;
2733
using WebViewRenderer = Microsoft.Maui.Controls.Compatibility.Platform.iOS.WkWebViewRenderer;
2834
using NavigationPageRenderer = Microsoft.Maui.Controls.Compatibility.Platform.iOS.NavigationRenderer;
2935
using TabbedPageRenderer = Microsoft.Maui.Controls.Compatibility.Platform.iOS.TabbedRenderer;
@@ -32,10 +38,6 @@
3238
using DefaultRenderer = Microsoft.Maui.Controls.Compatibility.Platform.iOS.Platform.DefaultRenderer;
3339
#endif
3440

35-
using Microsoft.Maui.Hosting;
36-
using Microsoft.Maui.Controls.Shapes;
37-
using Microsoft.Maui.LifecycleEvents;
38-
3941
namespace Microsoft.Maui.Controls.Hosting
4042
{
4143
public static class AppHostBuilderExtensions
@@ -70,33 +72,34 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
7072
{
7173
#if __ANDROID__
7274
events.AddAndroid(android => android
73-
.OnCreate((a, b) =>
74-
{
75-
// This just gets Forms Compat bits setup with what it needs
76-
// to initialize the first view. MauiContext hasn't been initialized at this point
77-
// so we setup one that will look exactly the same just
78-
// to make legacy Forms bits happy
79-
var services = MauiApplication.Current.Services;
80-
MauiContext mauiContext = new MauiContext(services, a);
81-
ActivationState state = new ActivationState(mauiContext, b);
82-
Forms.Init(new ActivationState(mauiContext, b), new InitializationOptions() { Flags = InitializationFlags.SkipRenderers });
83-
})
84-
.OnPostCreate((_, b) =>
85-
{
86-
// This calls Init again so that the MauiContext that's part of
87-
// Forms.Init matches the rest of the maui application
88-
var mauiApp = MauiApplication.Current.Application;
89-
if (mauiApp.Windows.Count > 0)
90-
{
91-
var window = mauiApp.Windows[0];
92-
var mauiContext = window.Handler?.MauiContext ?? window.View.Handler?.MauiContext;
75+
.OnCreate((a, b) =>
76+
{
77+
// This just gets Forms Compat bits setup with what it needs
78+
// to initialize the first view. MauiContext hasn't been initialized at this point
79+
// so we setup one that will look exactly the same just
80+
// to make legacy Forms bits happy
81+
var services = MauiApplication.Current.Services;
82+
MauiContext mauiContext = new MauiContext(services, a);
83+
ActivationState state = new ActivationState(mauiContext, b);
84+
Forms.Init(new ActivationState(mauiContext, b), new InitializationOptions() { Flags = InitializationFlags.SkipRenderers });
85+
GraphicsPlatform.RegisterGlobalService(NativeGraphicsService.Instance);
86+
})
87+
.OnPostCreate((_, b) =>
88+
{
89+
// This calls Init again so that the MauiContext that's part of
90+
// Forms.Init matches the rest of the maui application
91+
var mauiApp = MauiApplication.Current.Application;
92+
if (mauiApp.Windows.Count > 0)
93+
{
94+
var window = mauiApp.Windows[0];
95+
var mauiContext = window.Handler?.MauiContext ?? window.View.Handler?.MauiContext;
9396

94-
if (mauiContext != null)
95-
{
96-
Forms.Init(new ActivationState(mauiContext, b));
97-
}
98-
}
99-
}));
97+
if (mauiContext != null)
98+
{
99+
Forms.Init(new ActivationState(mauiContext, b));
100+
}
101+
}
102+
}));
100103
#elif __IOS__
101104
events.AddiOS(iOS =>
102105
{
@@ -112,6 +115,7 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
112115
// This calls Init again so that the MauiContext that's part of
113116
// Forms.Init matches the rest of the maui application
114117
var mauiApp = MauiUIApplicationDelegate.Current.Application;
118+
115119
if (mauiApp.Windows.Count > 0)
116120
{
117121
var window = mauiApp.Windows[0];
@@ -122,38 +126,42 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
122126
Forms.Init(new ActivationState(mauiContext));
123127
}
124128
}
129+
130+
GraphicsPlatform.RegisterGlobalService(NativeGraphicsService.Instance);
131+
125132
return true;
126133
});
127134
});
128135
#elif WINDOWS
129136
events.AddWindows(windows => windows
130-
.OnLaunching((_, args) =>
131-
{
132-
// We need to call Forms.Init so the Window and Root Page can new up successfully
133-
// The dispatcher that's inside of Forms.Init needs to be setup before the initial
134-
// window and root page start creating
135-
// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
136-
// MauiContext inside Forms
137-
MauiContext mauiContext = new MauiContext(MauiWinUIApplication.Current.Services, new UI.Xaml.Window());
138-
ActivationState state = new ActivationState(mauiContext, args);
139-
Forms.Init(state, new InitializationOptions() { Flags = InitializationFlags.SkipRenderers });
140-
})
141-
.OnLaunched((_, args) =>
142-
{
143-
// This calls Init again so that the MauiContext that's part of
144-
// Forms.Init matches the rest of the maui application
145-
var mauiApp = MauiWinUIApplication.Current.Application;
146-
if (mauiApp.Windows.Count > 0)
147-
{
148-
var window = mauiApp.Windows[0];
149-
var mauiContext = window.Handler?.MauiContext ?? window.View.Handler?.MauiContext;
137+
.OnLaunching((_, args) =>
138+
{
139+
// We need to call Forms.Init so the Window and Root Page can new up successfully
140+
// The dispatcher that's inside of Forms.Init needs to be setup before the initial
141+
// window and root page start creating
142+
// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
143+
// MauiContext inside Forms
144+
MauiContext mauiContext = new MauiContext(MauiWinUIApplication.Current.Services, new UI.Xaml.Window());
145+
ActivationState state = new ActivationState(mauiContext, args);
146+
Forms.Init(state, new InitializationOptions() { Flags = InitializationFlags.SkipRenderers });
147+
GraphicsPlatform.RegisterGlobalService(W2DGraphicsService.Instance);
148+
})
149+
.OnLaunched((_, args) =>
150+
{
151+
// This calls Init again so that the MauiContext that's part of
152+
// Forms.Init matches the rest of the maui application
153+
var mauiApp = MauiWinUIApplication.Current.Application;
154+
if (mauiApp.Windows.Count > 0)
155+
{
156+
var window = mauiApp.Windows[0];
157+
var mauiContext = window.Handler?.MauiContext ?? window.View.Handler?.MauiContext;
150158

151-
if (mauiContext != null)
152-
{
153-
Forms.Init(new ActivationState(mauiContext, args));
154-
}
155-
}
156-
}));
159+
if (mauiContext != null)
160+
{
161+
Forms.Init(new ActivationState(mauiContext, args));
162+
}
163+
}
164+
}));
157165
#endif
158166
});
159167

@@ -182,7 +190,7 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
182190
handlers.TryAddCompatibilityRenderer(typeof(Line), typeof(LineRenderer));
183191
handlers.TryAddCompatibilityRenderer(typeof(Polyline), typeof(PolylineRenderer));
184192
handlers.TryAddCompatibilityRenderer(typeof(Polygon), typeof(PolygonRenderer));
185-
handlers.TryAddCompatibilityRenderer(typeof(Rectangle), typeof(RectangleRenderer));
193+
handlers.TryAddCompatibilityRenderer(typeof(Shapes.Rectangle), typeof(RectangleRenderer));
186194
handlers.TryAddCompatibilityRenderer(typeof(RadioButton), typeof(RadioButtonRenderer));
187195
handlers.TryAddCompatibilityRenderer(typeof(Slider), typeof(SliderRenderer));
188196
handlers.TryAddCompatibilityRenderer(typeof(WebView), typeof(WebViewRenderer));

0 commit comments

Comments
 (0)