.NET MAUI currently uses the CreateScope feature of MS.Ext.DI to create services that are scoped to the .NET MAUI concept of a window (AppCompatActivity, UIScene, UI.XAML.Window).
We also have an internal scoping mechanism that we use to scope aspects of the platform application with a lifecycle that isn't tied to a Window.
When a new Window is created we take the Application level IServiceProvider
and generate a new scope.
This allows us to register things that need to be scoped to a window (IDispatcher
, ITicket
, IAnimationManager
)
???
(WrappedServiceProvider
)[
maui/src/Core/src/MauiContext.cs
Line 47
in
b09979d
]
maui/src/Core/src/MauiContext.cs
Line 47 in b09979d
class WrappedServiceProvider : IServiceProvider
The purpose of this class is to intercept GetServices
requests and return anything that we've registered as scoped to this particular WrappedServiceProvider
. The primary reason for us needing to do this is that the Ms.Ext.DI ServiceProvider
implementation only allows you to create a single level of scoping but we need a mechanism to create types that are scoped to particular areas of a window.
We use this feature for ModalPages
. Because ModalPages
are basically a "Virtual Window" we use the WrappedServiceProvider
to create sets of types that need to be scoped to this new Virtual Window instead of the actual window. For example things like the toolbar or top level container the user sees are all scoped to the "Virtual Window".
This scoping feature is very useful within Android because with (Android)[
] you will get areas of your View that have to use features of a parent view opposed to those same features on the window. Think of it like needing different scopes for each context that exists within a window.- Window Scope (this is for views created at the window level)
- Tab Scope (this is for views that are created inside each Tab.)
- Navigation Scope (this is for views that are created inside the Navigation)
Any services registered with the WrappedServiceProvider
don't participate in the resolution used by the Ms.Ext.DI. The way the WrappedServiceProvider
works is that it checks an internal dictionary for a requested Type
. If it doesn't find that type then it passes the request to whatever it's wrapped. So on and so forth until it reaches window scoped IServiceProvider