Provide Static MauiContextAccessor
See original GitHub issueDescription
The root application types used by Maui are prescriptive in accessing a Current instance to get the Application Scope IServiceProvider and a reference to IApplication. While this works for Maui Applications it causes problems in Embedding scenarios where the Android MauiApplication or the Windows MauiWinUIApplication for instance will not exist.
(Public) API Changes
static MauiContextAccessor
Properties
API | Description |
---|---|
Services | Gets or sets the IServiceProvider. |
Application | Gets or sets the IApplication. |
Usage Scenarios
This provides a single class that can be accessed from:
- Markup Extensions
- Embedding Scenarios
- Existing native Maui Application classes
Backward Compatibility
This can be added so that under the covers the existing native Maui Application classes such as the Android MauiApplication or the MauiWinUIApplication can use this when getting/setting the Services/Application properties.
public abstract class MauiWinUIApplication : UI.Xaml.Application, IPlatformApplication
{
public IServiceProvider Services
{
get => MauiContextAccessor.Services;
protected set => MauiContextAccessor.Services = value;
}
public IApplication Application
{
get => MauiContextAccessor.Application;
protected set => MauiContextAccessor.Application = value;
}
}
Difficulty
Low
Issue Analytics
- State:
- Created a month ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Dependency Injection from static class : r/dotnetMAUI
IO am currently porting my Xamarin Forms Project to .net Maui. In my Xamarin Forms App i had a ViewModelLocator as shown in...
Read more >Creating and Using HTTP Client SDKs in .NET 6 - InfoQ
The goal here is to give consumers the fastest way possible to access an existing API. Create a static factory method that creates...
Read more >NET MAUI: How to bind a static property to a xaml control?
Get the full .NET MAUI Course: https://courses.devs.school/courses/net- maui In this video, I am going to show you a way to use a static...
Read more >How to Get HttpContext ASP.NET Core
In this article, we will discuss what HttpContext is and how to use it in a controller, plus how to register and use...
Read more >ASP.NET Core Blazor Hybrid static files
In .NET MAUI apps, raw assets using the MauiAsset build action and .NET MAUI file system helpers are used for static assets.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@dansiegel yeah thanks I did see that other PR. I’m not sure I’m a big fan of that either because of the problems it can create, but I guess it was already there…
Hmm this is somewhat of an anti-pattern. The whole idea is to never have static instances of these available. But if you do want one, isn’t there some kind of
Application.Current.<something>
that you could use?