Plugin.Maui.Theme 9.2.1

dotnet add package Plugin.Maui.Theme --version 9.2.1
                    
NuGet\Install-Package Plugin.Maui.Theme -Version 9.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Plugin.Maui.Theme" Version="9.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Plugin.Maui.Theme" Version="9.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Plugin.Maui.Theme" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Plugin.Maui.Theme --version 9.2.1
                    
#r "nuget: Plugin.Maui.Theme, 9.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Plugin.Maui.Theme@9.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Plugin.Maui.Theme&version=9.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Plugin.Maui.Theme&version=9.2.1
                    
Install as a Cake Tool

🌈 Plugin.Maui.Theme

The Plugin.Maui.Theme library provides a powerful theme management service for .NET MAUI applications, allowing dynamic switching of visual styles.


🔧 Class ThemeService

A singleton service that manages application themes via ResourceDictionary.

📦 Properties and Fields

  • ThemeService.Current: Static property to access the current instance.
  • _themes: Dictionary containing registered themes (Dictionary<int, ResourceDictionary>).
  • _appResources: Reference to the application's MergedDictionaries.
  • _currentDict: Currently applied ResourceDictionary.
  • _currentTheme: Key-value pair representing the active theme.

🔑 Метод InitAppResources

Initializes a reference to the application's MergedDictionaries.

public void InitAppResources(ResourceDictionary resource)

💥Throws ArgumentNullException if the resource is null.


🎨 Method AddTheme

Registers a theme in the collection.

AddTheme is deprecated. Please use AddThemes.

public void AddTheme(int key, ResourceDictionary theme, bool isDefault = false)
  • key: Unique identifier for the theme.
  • theme: ResourceDictionary defining the theme.
  • isDefault: If true, sets the theme as the default.

🎨 Method AddThemes

The AddThemes method provides a convenient way to register multiple themes within the application without requiring explicit keys for each.

public IThemeService AddThemes(params ResourceDictionary[] resources)    or
public IThemeService AddThemes(params KeyValuePair<int, ResourceDictionary>[] resources)

This method is designed for scenarios where you have a collection of ResourceDictionary instances that you wish to register as themes, and you're comfortable with the system automatically assigning sequential keys to them, starting from the next available index.

Parameters

  • resources: An array of ResourceDictionary objects that will be registered as themes.

Returns

  • The current IThemeService instance. This allows for fluent chaining of method calls, enabling you to perform multiple theme-related operations in a concise manner (e.g., themeService.AddThemes(...).SetCurrentTheme(...)).

🔄 Method ChangeTheme

Switches the application to the specified theme.

public void ChangeTheme(int key)

🧯 Throws exceptions if the theme is not found or InitAppResources was not called.


⚙️ Method InitService

Finalizes service setup.

public void InitService()
  • If the theme is already in MergedDictionaries, it becomes active.
  • Otherwise, the default or first available theme is applied.

📡 Event ThemeChanged

Triggered whenever the theme is changed:

public event EventHandler<ThemeChangedEventArgs>? ThemeChanged;
  • Arguments include KeyTheme and NewTheme.

🧾 Record ThemeChangedEventArgs

public record ThemeChangedEventArgs(int KeyTheme, ResourceDictionary NewTheme);

Used to notify subscribers about theme changes.


What is a DynamicResource?

A DynamicResource is a reference to a resource that is resolved just before it's used. This means that if the value of the resource changes while the application is running, all elements using that DynamicResource will automatically update.

Unlike a StaticResource, which is resolved only once when the application starts, a DynamicResource provides flexibility and allows for a dynamic user interface.

How to Use a DynamicResource

Using a DynamicResource is very simple. You just need to specify the resource key in your XAML markup.


Behavior of Theme Addition

When using the AddThemes methods, it's important to understand how keys are assigned to the styles.

If you first call:

public IThemeService AddThemes(params KeyValuePair<int, ResourceDictionary>[] resources)

And then call:

public IThemeService AddThemes(params ResourceDictionary[] resources)

The key for the second set of resources will be calculated using the formula: maximum_key + 1, where maximum_key is the highest numerical key passed in the first call.

Example:

If your first call included themes with keys 1 and 5, the first resource in the second call will automatically be assigned the key 6.


✅ Usage Example

In the App.xaml.cs class, you can use ThemeService like this:

		public static MauiApp CreateMauiApp()
		{
			var builder = MauiApp.CreateBuilder();
			builder
				.UseMauiApp<App>()
				.ConfigureFonts();
				// Your theme service initialization
				 // This line assumes 'UseTheme()' is an extension method
				  // on MauiAppBuilder or configures a theme service.
				   builder.UseTheme(); // <-- Call your UseTheme() method here

			return builder.Build();
		}

public App(IThemeService themeService)
{
	InitializeComponent();

	themeService.InitAppResources(Resources)
				.AddThemes(new KeyValuePair<int, ResourceDictionary>(99, new RedTheme()))
				.AddThemes(new BlueTheme(), new DarkTheme());

	themeService.ThemeChanged += Current_ThemeChanged;

}

🧠 Highlights

  • Supports multiple themes.
  • Enables dynamic switching.
  • Event-driven UI updates.

🛠 Author: Fishman 📅 Last updated: July 2025

Acknowledgments

We're thrilled to announce that the preparation of version 9.2.0 was significantly supported by Алексом Добрыниным. His contributions were invaluable in bringing this release to fruition.

📄 License

This project is licensed under the Boost Software License – Version 1.0 – August 17th, 2003.


Product Compatible and additional computed target framework versions.
.NET net9.0-android35.0 is compatible.  net9.0-ios18.0 is compatible.  net9.0-maccatalyst18.0 is compatible.  net9.0-windows10.0.19041 is compatible.  net10.0-android was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.2.1 26 7/27/2025
9.2.0 18 7/19/2025
9.0.1 131 7/13/2025

public static MauiApp CreateMauiApp()
 {
  var builder = MauiApp.CreateBuilder();
  builder
   .UseMauiApp<App>()
   .ConfigureFonts();
   // Your theme service initialization
    // This line assumes 'UseTheme()' is an extension method
     // on MauiAppBuilder or configures a theme service.
      builder.UseTheme(); // <-- Call your UseTheme() method here

  return builder.Build();
 }

public App(IThemeService themeService)
{
InitializeComponent();

themeService.InitAppResources(Resources)
   .AddThemes(new KeyValuePair<int, ResourceDictionary>(99, new RedTheme()))
   .AddThemes(new BlueTheme(), new DarkTheme());

themeService.ThemeChanged += Current_ThemeChanged;

}