question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide Static MauiContextAccessor

See original GitHub issue

Description

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:open
  • Created a month ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Eiloncommented, Aug 18, 2023

@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…

1reaction
Eiloncommented, Aug 16, 2023

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found