Can't use App.xaml resources after Dependency Injection .NET MAUI
See original GitHub issueDescription
I have an application that uses mvvm pattern with Community Toolkit features. I am trying to use Dependency Injection to use an ApiService with its interface in my viewmodels, but after following the steps described here, I can’t access to App.xaml Resources (specifically colors), the intellisense works when I am writing code in XAML, but it doesn´t work after running. It is important to notice that I was using colors from resources correctly before trying to use Dependency Injection and changing the ViewModel - View linking method to the one described here, but I was unable to use ApiService. Here is my code.
Steps to Reproduce
App.xaml.cs (Login page is my first page):
public App(LoginPage page)
{
InitializeComponent();
MainPage = page;
}
LoginPage.xaml.cs
public partial class LoginPage : ContentPage
{
public LoginPage(LoginPageViewModel loginPageViewModel)
{
BindingContext = loginPageViewModel;
InitializeComponent();
}
}
LoginPageViewModel
public LoginPageViewModel(IApiService apiService)
{
_apiService = apiService;
Opacity = 1f;
IsEnabled = true;
IsRunning = false;
}
LoginPage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="NewScholarApp.Views.LoginPage"
Title="LoginPage"
xmlns:vm="clr-namespace:NewScholarApp.ViewModels"
x:DataType="vm:LoginPageViewModel"
BackgroundColor="{StaticResource GreenSchool}">
RegisterViews and ViewModels
public static MauiAppBuilder ConfigureViews(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddTransient<LoginPage>();
return mauiAppBuilder;
}
public static class RegisterViewModels
{
public static MauiAppBuilder ConfigureViewModels(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddSingleton<LoginPageViewModel>();
return mauiAppBuilder;
}
}
Link to public reproduction project repository
https://github.com/luis95gr/SampleProject
Version with bug
7.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android, I was not able test on other platforms
Affected platform versions
Android 12.1
Did you find any workaround?
Don’t use any StaticResource
Relevant log output
Microsoft.Maui.Controls.Xaml.XamlParseException: 'Position 8:14. StaticResource not found for key GreenSchool'
Issue Analytics
- State:
- Created 10 months ago
- Reactions:5
- Comments:19 (2 by maintainers)

Top Related StackOverflow Question
What is happening is that if you use DI to register ContentPage elements and maybe other UI elements, you will not be able to access static resources declared in your App.xaml file. I’m sure the technical reasons involve the app lifecycle assumptions deep within MAUI. A UI element resolved via constructor injection is initialized very early in the app or page lifecycle, and it can only access those resources later on. I believe I saw other bugs also logged about this, and maybe it will get fixed.
But essentially you can get around this by injecting IServiceProvider into the App class, and then using that to resolve the MainPage inside the App class, thereby forcing the resolution of the page element to occur later than the constructor injection would’ve resolved it. I demonstrated this approach in the comment above yours.
Another benefit of that approach is if you were injecting multiple modals/elements/popups into the same class, you would be able to ensure that those modals/elements/popups were only instantiated if needed, and only when they are needed. So you could paint the screen quicker, use less memory, etc.
I encountered this same issue when upgrading to MAUI and switching to the new DI style. This is a bug.
The options for workarounds I found are the following: