@@ -72,93 +72,125 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
72
72
{
73
73
#if __ANDROID__
74
74
events . AddAndroid ( android => android
75
- . OnCreate ( ( a , b ) =>
75
+ . OnApplicationCreating ( ( app ) =>
76
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
77
+ // This is the initial Init to set up any system services registered by
78
+ // Forms.Init(). This happens in the Application's OnCreate - before
79
+ // any UI has appeared.
80
+ // This creates a dummy MauiContext that wraps the Application.
81
+
81
82
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 } ) ;
83
+ var mauiContext = new MauiContext ( services , app ) ;
84
+ var state = new ActivationState ( mauiContext ) ;
85
+ Forms . Init ( state , new InitializationOptions { Flags = InitializationFlags . SkipRenderers } ) ;
86
+
85
87
GraphicsPlatform . RegisterGlobalService ( NativeGraphicsService . Instance ) ;
86
88
} )
87
- . OnPostCreate ( ( _ , b ) =>
89
+ . OnCreate ( ( activity , bundle ) =>
90
+ {
91
+ // This is the Init that sets up the first context from the activity.
92
+ // There is still no official MauiContext since that happens just after this.
93
+
94
+ var services = MauiApplication . Current . Services ;
95
+ var mauiContext = new MauiContext ( services , activity ) ;
96
+ var state = new ActivationState ( mauiContext , bundle ) ;
97
+ Forms . Init ( state , new InitializationOptions { Flags = InitializationFlags . SkipRenderers } ) ;
98
+ } )
99
+ . OnPostCreate ( ( activity , bundle ) =>
88
100
{
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 )
101
+ // This is the final Init that ensures the Forms type is using the same
102
+ // MauiContext that is part of the rest of the maui application.
103
+
104
+ var windows = Application . Current ? . Windows ;
105
+ if ( windows ? . Count > 0 )
93
106
{
94
- var window = mauiApp . Windows [ 0 ] ;
95
- var mauiContext = window . Handler ? . MauiContext ?? window . View . Handler ? . MauiContext ;
107
+ var window = windows [ 0 ] ;
108
+ var mauiContext =
109
+ window . Handler ? . MauiContext ??
110
+ window . Page ? . Handler ? . MauiContext ;
96
111
97
112
if ( mauiContext != null )
98
113
{
99
- Forms . Init ( new ActivationState ( mauiContext , b ) ) ;
114
+ var state = new ActivationState ( mauiContext , bundle ) ;
115
+ Forms . Init ( state ) ;
100
116
}
101
117
}
102
118
} ) ) ;
103
119
#elif __IOS__
104
- events . AddiOS ( iOS =>
105
- {
106
- iOS . WillFinishLaunching ( ( x , y ) =>
120
+ events . AddiOS ( iOS => iOS
121
+ . WillFinishLaunching ( ( app , options ) =>
107
122
{
108
- MauiContext mauiContext = new MauiContext ( MauiUIApplicationDelegate . Current . Services , new UIKit . UIWindow ( ) ) ;
109
- Forms . Init ( new ActivationState ( mauiContext ) , new InitializationOptions ( ) { Flags = InitializationFlags . SkipRenderers } ) ;
110
- return true ;
111
- } ) ;
123
+ // This is the initial Init to set up any system services registered by
124
+ // Forms.Init(). This happens before any UI has appeared.
125
+ // This creates a dummy MauiContext.
112
126
113
- iOS . FinishedLaunching ( ( x , y ) =>
127
+ var services = MauiUIApplicationDelegate . Current . Services ;
128
+ var mauiContext = new MauiContext ( services ) ;
129
+ var state = new ActivationState ( mauiContext ) ;
130
+ Forms . Init ( state , new InitializationOptions { Flags = InitializationFlags . SkipRenderers } ) ;
131
+ return true ;
132
+ } )
133
+ . FinishedLaunching ( ( app , options ) =>
114
134
{
115
- // This calls Init again so that the MauiContext that's part of
116
- // Forms.Init matches the rest of the maui application
117
- var mauiApp = MauiUIApplicationDelegate . Current . Application ;
135
+ // This is the final Init that ensures the Forms type is using the same
136
+ // MauiContext that is part of the rest of the maui application.
118
137
119
- if ( mauiApp . Windows . Count > 0 )
138
+ var windows = Application . Current ? . Windows ;
139
+ if ( windows ? . Count > 0 )
120
140
{
121
- var window = mauiApp . Windows [ 0 ] ;
122
- var mauiContext = window . Handler ? . MauiContext ?? window . View . Handler ? . MauiContext ;
141
+ var window = windows [ 0 ] ;
142
+ var mauiContext =
143
+ window . Handler ? . MauiContext ??
144
+ window . Page ? . Handler ? . MauiContext ;
123
145
124
146
if ( mauiContext != null )
125
147
{
126
- Forms . Init ( new ActivationState ( mauiContext ) ) ;
148
+ var state = new ActivationState ( mauiContext ) ;
149
+ Forms . Init ( state ) ;
127
150
}
128
151
}
129
-
152
+
130
153
GraphicsPlatform . RegisterGlobalService ( NativeGraphicsService . Instance ) ;
131
154
132
155
return true ;
133
- } ) ;
134
- } ) ;
156
+ } ) ) ;
135
157
#elif WINDOWS
136
158
events . AddWindows ( windows => windows
137
- . OnLaunching ( ( _ , args ) =>
159
+ . OnLaunching ( ( app , args ) =>
138
160
{
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
161
+ // This is the initial Init to set up any system services registered by
162
+ // Forms.Init(). This happens before any UI has appeared.
163
+ // This creates a dummy MauiContext.
164
+ // We need to call this so the Window and Root Page can new up successfully
165
+ // The dispatcher that's inside of Forms.Init needs to be setup before the initial
166
+ // window and root page start creating.
142
167
// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
143
168
// MauiContext inside Forms
144
- MauiContext mauiContext = new MauiContext ( MauiWinUIApplication . Current . Services , new UI . Xaml . Window ( ) ) ;
145
- ActivationState state = new ActivationState ( mauiContext , args ) ;
169
+
170
+ var services = MauiWinUIApplication . Current . Services ;
171
+ var mauiContext = new MauiContext ( services ) ;
172
+ var state = new ActivationState ( mauiContext , args ) ;
146
173
Forms . Init ( state , new InitializationOptions ( ) { Flags = InitializationFlags . SkipRenderers } ) ;
174
+
147
175
GraphicsPlatform . RegisterGlobalService ( W2DGraphicsService . Instance ) ;
148
176
} )
149
- . OnLaunched ( ( _ , args ) =>
177
+ . OnLaunched ( ( app , args ) =>
150
178
{
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 )
179
+ // This is the final Init that ensures the Forms type is using the same
180
+ // MauiContext that is part of the rest of the maui application.
181
+
182
+ var windows = Application . Current ? . Windows ;
183
+ if ( windows ? . Count > 0 )
155
184
{
156
- var window = mauiApp . Windows [ 0 ] ;
157
- var mauiContext = window . Handler ? . MauiContext ?? window . View . Handler ? . MauiContext ;
185
+ var window = windows [ 0 ] ;
186
+ var mauiContext =
187
+ window . Handler ? . MauiContext ??
188
+ window . Page ? . Handler ? . MauiContext ;
158
189
159
190
if ( mauiContext != null )
160
191
{
161
- Forms . Init ( new ActivationState ( mauiContext , args ) ) ;
192
+ var state = new ActivationState ( mauiContext , args ) ;
193
+ Forms . Init ( state ) ;
162
194
}
163
195
}
164
196
} ) ) ;
0 commit comments